The quickest way to get start with Node.Js, Express & MongoDB, just clone the project:
$ git clone https://github.com/arjunkhetia/Node-Task.git
Install dependencies:
$ npm install
Start Express.js app at http://localhost:5000/
:
$ npm start
Nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
Start Express.js app with nodemon at http://localhost:5000/
:
$ nodemon bin/www
Node PortFinder is a tool to find an open port or domain socket on the machine.
var portfinder = require('portfinder');
var port = 5000;
var portSpan = 999;
portfinder.getPort({
port: port, // minimum port number
stopPort: port + portSpan // maximum port number
}, function (err, openPort) {
if (err) throw err;
port = openPort;
});
Node.js runs in a single process, by default. Ideally, we want one process for each CPU core, so we can distribute the workload across all the cores. Hence improving the scalability of web apps handling HTTP requests and performance in general. In addition to this, if one worker crashes, the others are still available to handle requests.
var cluster = require('cluster');
var workers = process.env.WORKERS || require('os').cpus().length;
if (cluster.isMaster) {
console.log('Master cluster is running on %s with %s workers', process.pid, workers);
for (var i = 0; i < workers; ++i) {
var worker = cluster.fork().process;
console.log('worker %s on %s started', i+1, worker.pid);
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker %s died. restarting...', worker.process.pid);
cluster.fork();
});
}
if (cluster.isWorker) {
// Server code
}
Morgan - HTTP request logger middleware for node.js:
var logger = require('morgan');
app.use(logger('dev'));
app.use(logger(':remote-addr :remote-user :datetime :req[header] :method :url HTTP/:http-version :status :res[content-length] :res[header] :response-time[digits] :referrer :user-agent', {
stream: accessLogStream
}));
Winston - is designed to be a simple and universal logging library with support for multiple transports:
var winston = require('winston');
var logger = winston.createLogger({
format: winston.format.combine(
winston.format.colorize({
all: true
}),
winston.format.printf(
data => `${data.level} : ${data.message}`
)
),
transports: [
new winston.transports.Console({
level: 'silly'
}),
new winston.transports.File({
level: 'silly',
filename: './log/ServerData.log'
})
]
});
To provide an automated rotation of Express/Connect logs or anything else that writes to a log on a regular basis that needs to be rotated based on date.
var rfs = require('rotating-file-stream');
var accessLogStream = rfs('file.log', {
size: '10M', // rotate every 10 MegaBytes written
interval: '1d', // rotate daily
compress: 'gzip' // compress rotated files
path: 'log' // folder path for log files
});
MongoDB driver for Node.js. A Connection Pool is a cache of database connections maintained by your driver so that connections can be re-used when new connections to the database are required.
var MongoClient = require('mongodb').MongoClient;
var option = {
reconnectTries : 5, // Server attempt to reconnect.
reconnectInterval: 1000, // Server will wait between retries.
keepAlive: true, // Keep connection alive.
poolSize : 10, // The maximum number of connections to create at once.
connectTimeoutMS: 5000 // TCP Connection timeout setting.
};
Express Status Monitor is simple, self-hosted module based on Socket.io and Chart.js to report realtime server metrics for Express-based ode servers.
app.use(require('express-status-monitor')({
title: 'Server Status', // title for status screen
path: '/status', // path for server status invokation
spans: [{
interval: 1, // every second
retention: 60 // keep 60 datapoints in memory
}],
chartVisibility: {
cpu: true, // enable CPU Usage
mem: true, // enable Memory Usage
load: true, // enable One Minute Load Avg
responseTime: true, // enable Response Time
rps: true, // enable Requests per Second
statusCodes: true // enable Status Codes
},
healthChecks: [{
protocol: 'http', // protocol
host: 'localhost' // server host name
path: '/users', // endpoint to check status
port: '5000' // server port
}] // health check will be considered successful if the endpoint returns a 200 status code
}));