Reading txt File in chunk in nodeJs using stream (updated : added pause and resume)

var fs = require('fs');

var stream = fs.createReadStream('data.json');


//stream.pause(); To pause the streaming
//stream.resume(); To resume the streaming


stream.on('data', function (chunk) {


console.log('-----------------START----------------');
console.log(chunk.toString());
console.log('----------------FINISH----------------');

});


stream.on('data', function (chunk) {

console.log('CHUNK LENGTH WAS: ' + chunk.length);

});

stream.on('end', function () {

console.log('----------------END OF THE FILE----------------');

});

Create getter and setter module in NodeJS

var number, origin, destination;

exports.setNumber = function (num) {
number = num;
};

exports.setOrigin = function (o) {
origin = o;
};

exports.setDestination = function (d) {
destination = d;
};

exports.getInfo = function () {
return {
number      : number,
origin      : origin,
destination : destination
};
};

==============================
user in app.js


var flight = require('./flight');

flight.setOrigin('Kensington');
flight.setDestination('London');
flight.setNumber(356);

console.log(flight.getInfo());

Enable cross origin ( CORS ) in NodeJs

app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "X-Requested-With,X-Powered-By,Content-Type");
if (req.method === 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});