Skip to content

Repository files navigation

Node Express MySQL Project Version

Linux BuildWindows BuildNSP StatusTest CoverageDependency StatusdevDependencies Status

The quickest way to get start with Node.Js, Express & MySQL, just clone the project:

$ git clone https://github.com/arjunkhetia/Node.Js-Express-MySQL-Project.git

Install dependencies:

$ npm install

Start Express.js app at http://localhost:3000/:

$ npm start

Nodemon

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:3000/:

$ nodemon bin/www

Node PortFinder

Node PortFinder is a tool to find an open port or domain socket on the machine.

varportfinder=require('portfinder');varport=3000;varportSpan=999;portfinder.getPort({port: port,// minimum port numberstopPort: port+portSpan// maximum port number},function(err,openPort){if(err)throwerr;port=openPort;});

Nodejs Cluster

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.

varcluster=require('cluster');varworkers=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(vari=0;i<workers;++i){varworker=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}

Logger - Morgan & Winston

Morgan - HTTP request logger middleware for node.js:

varlogger=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:

varwinston=require('winston');varlogger=winston.createLogger({format: winston.format.combine(winston.format.colorize({all: true}),winston.format.printf(data=>`${data.level} : ${data.message}`)),transports: [newwinston.transports.Console({level: 'silly'}),newwinston.transports.File({level: 'silly',filename: './log/ServerData.log'})]});

Rotating File Stream

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.

varrfs=require('rotating-file-stream');varaccessLogStream=rfs('file.log',{size: '10M',// rotate every 10 MegaBytes writteninterval: '1d',// rotate dailycompress: 'gzip'// compress rotated filespath: 'log'// folder path for log files});

MySQL Database Connectivity (with connection pool)

This is a node.js driver for mysql. Also implemented the connection pool, it is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database.

varmysql=require('mysql');varpool=mysql.createPool({connectionLimit : 50,// The maximum number of connections to create at once. (Default: 10)queueLimit: 100,// The maximum number of connection requests the pool will queue before returning an error from getConnection. (Default: 0)host : '127.0.0.1',// The hostname of the database you are connecting to. (Default: localhost)port : 3306,// The port number to connect to. (Default: 3306)user : 'arjun',// The MySQL user to authenticate as.password : '',// The password of that MySQL user.database : 'mysqldb',// Name of the database to use for this connection.connectTimeout : 10000,// The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10000)waitForConnections: true,// Determines the pool's action when no connections are available and the limit has been reached. (Default: true)acquireTimeout: 10000,// The milliseconds before a timeout occurs during the connection acquisition. (Default: 10000)debug : false// Prints protocol details to stdout. (Default: false)});

Server Status Monitor

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 screenpath: '/status',// path for server status invokationspans: [{interval: 1,// every secondretention: 60// keep 60 datapoints in memory},{interval: 5,// every 5 secondsretention: 60}],chartVisibility: {cpu: true,// enable CPU Usagemem: true,// enable Memory Usageload: true,// enable One Minute Load AvgeventLoop: true,// enable EventLoop Precess Usageheap: true,// enable Heap Memory UsageresponseTime: true,// enable Response Timerps: true,// enable Requests per SecondstatusCodes: true// enable Status Codes},healthChecks: [{protocol: 'http',// protocolhost: 'localhost'// server host namepath: '/users',// endpoint to check statusport: '3000'// server port}],// health check will be considered successful if the endpoint returns a 200 status codeignoreStartsWith: '/admin'// ignore path starts with}));

Monitoring Page

About

Node.Js + Express + MySQL - Template

Resources

Stars

31 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages