Friday, 17 March 2017

Node js Tutorial for beginners with examples - page 4

Node js Tutorial for beginners with examples - page 4

Question: How to print a stack trace in Node.js?
console.trace("print me")



Question: How can I get the full object in Node.js's console.log()?
const util = require('util');
console.log(util.inspect(myObject, false, null))



Question: How to use jQuery with Node.js?
First instal the jquery then use as following.
require("jsdom").env("", function(err, window) {
    if (err) {
        console.error(err);
        return;
    }

    var $ = require("jquery")(window);
});



Question: How to change bower's default components folder?
Create a .bowerrc file in root and add following code.
{
  "directory" : "public/components"
}



Question: How to encode base64 in nodeJS?
console.log(new Buffer("hello").toString('base64')); //aGVsbG8=



Question: How to decode base64 in nodeJS?
console.log(new Buffer("aGVsbG8=", 'base64').toString('ascii')); //hello



Question: How to get listing of files in folder?
const testFolder = './files/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
})



Question: How do you extract POST data in Node.js?
app.use(express.bodyParser());
app.post('/', function(request, response){
    console.log(request.body.user.fname);  //print first name
    console.log(request.body.user.lname); //print last name
});



Question: How to remove file nodeJS?
var fs = require('fs');
var filePath = '/folder/arun.docx'; 
fs.unlinkSync(filePath);



Question: How to access the GET parameters after ? in Express?
var email = req.param('email');


Question: How to copy file in nodeJS?
var fs = require('fs');
fs.createReadStream('existing.txt').pipe(fs.createWriteStream('new_existing.txt'));



Question: How to append string in File?
var fs = require('fs');
fs.appendFile('message.txt', 'data to append', function (err) {

});



Question: How to get version of package?
var packageJSON = require('./package.json');
console.log(packageJSON.version);



Question: What is express js?
Express.js is a NodeJS framework.
It is designed for building single-page, multi-page, and hybrid web applications.


Question: How to Send emails in Node.js?
You can use node-email-templates.
https://github.com/crocodilejs/node-email-templates


Question: How to get Ip Address of client?
request.connection.remoteAddress



No comments :

Post a Comment