a blog for those who code

Tuesday 22 July 2014

5 Tips for Beginner Nodejs Developers

In this post we will show 5 common question's solutions for Beginner Nodejs developers which will be useful in day to day applications.


1. How to parse JSON using Nodejs

To parse JSON using Nodejs you can simply use JSON.parse().
The JSON.parse() method parses a string as JSON. Syntax of JSON.parse() is JSON.parse(text) where text : The string to parse as JSON. It returns the Object corresponding to the given JSON text.

2. How to print a stack trace in Nodejs

To print a stack trace in Nodejs you can simply use stack member of Error object.

For Example,

var stackTrace = new Error().stack;
console.log(stackTrace);

3. How to write in file in Nodejs

To write in file you have to use filestream.
For example,

var fs = require('fs')
fs.writeFile("File_Location", "Text_To_Write", fucntion(err)){
if(err) {
console.log(err);
} else {
console.log("Success");
}
});

4. How to load HTML in Nodejs

To load HTML in Nodejs we can use filestream library.
For example,

var fs = require('fs');
fs.readFile(filename, "binary", function(err, file) {
      if(err) {
        return;
      }

      response.writeHead(200, {"Content-Type": "text/html"});
      response.write(file);
      response.end();
    });

5. How to do Encryption in Nodejs

For doing Encryption in Nodejs we can use crypto module.
For example,

var crypto = require('crypto');
var value = crypto.createHmac('SHA256', 'key').update('string').digest('base64');

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment