Tag: nodejs

  • How to enable SSL/TLS for Express.js application

    For node.js applications, you can enable SSL on the application side or using Nginx or Apache running as a reverse proxy. To enable SSL for a node.js express application, use the following code.

    const fs = require('fs')
    const https = require('https')
    const express = require('express')
    
    var port = 3000;
    
    var options = {
        key: fs.readFileSync('./ssl/ssl.key'),
        cert: fs.readFileSync('./ssl/ssl.crt'),
    };
    
    var app = express();
    
    var server = https.createServer(options, app).listen(port, function(){
      console.log("Express server listening on port " + port);
    });
    
    app.get('/', function (req, res) {
        res.writeHead(200);
        res.end("hello world\n");
    });
    

    See node.js

  • Install Node.js on Ubuntu/Debian

    To install Node.js on Ubuntu, first set up repository as per your version requirements.

    For NodeJs v21.x

    curl -sL https://deb.nodesource.com/setup_21.x | bash -

    For NodeJs v20.x

    curl -sL https://deb.nodesource.com/setup_20.x | bash -

    For NodeJs v18.x

    curl -sL https://deb.nodesource.com/setup_18.x | bash -

    For NodeJs v17.x

    curl -sL https://deb.nodesource.com/setup_17.x | bash -

    For NodeJs v16.x

    curl -sL https://deb.nodesource.com/setup_16.x | bash -

    For NodeJs v15.x

    curl -sL https://deb.nodesource.com/setup_15.x | bash -

    For NodeJs v14.x

    curl -sL https://deb.nodesource.com/setup_14.x | bash -

    For NodeJs v12.x

    curl -sL https://deb.nodesource.com/setup_12.x | bash -

    For NodeJs v11.x

    curl -sL https://deb.nodesource.com/setup_11.x | bash -

    Node.js v10.x

    curl -sL https://deb.nodesource.com/setup_10.x | bash -

    Node.js v9.x

    curl -sL https://deb.nodesource.com/setup_9.x | bash -

    Node.js v8.x

    curl -sL https://deb.nodesource.com/setup_8.x | bash -

    Node.js v6.x

    curl -sL https://deb.nodesource.com/setup_6.x | bash -

    Now install node.js with

    apt-get install -y nodejs

    Install build-essential package

    apt install -y build-essential

    See Node.Js

  • nodemon

    Node.js applications when you run with nodejs command, need to restart when a change is made to the source code of the application.

    nodemon will watch your application source code for changes, and restart if a change is detected.

    To install nodemon, run

    npm install -g nodemon

    Example

    nodemon app.js 3000

    nodejs

  • pm2 process manager for node.js

    Auto Start pm2 on boot
    How to host static site using pm2

    pm2 is a process manager for node.js applications. It is similar to forever.

    http://pm2.keymetrics.io

    First you need to install npm, on Ubuntu/Debian, run

    apt install npm

    To install pm2, run

    npm install pm2 -g

    Start an Application

    pm2 start app.js

    Start an Application with name

    pm2 start app.js -n "app-name-here"

    Start npm

    pm2 start npm --name my-app -- run start

    Start the Application in Cluster mode with

    pm2 start app.js -n "app-name-here" -i 5

    To Scale a clustered application, run

    pm2 scale app-name-here 2

    Start the processes on reboot

    pm2 save
    pm2 startup

    List running applications

    pm2 ls

    Controlling running application

    pm2 stop    
    pm2 restart 
    pm2 delete

    Restart all running applications

    pm2 restart all

    nodejs

  • Install Node.js on CentOS Server

    You can find latest version of Node.js at

    https://nodejs.org/en/

    To install Node.js on CentOS 7/8

    Add node.js repository

    NodeJS 12.x

    curl -sL https://rpm.nodesource.com/setup_12.x | bash -
    

    NodeJS 11.x

    curl -sL https://rpm.nodesource.com/setup_11.x | bash -
    

    NodeJS 10.x

    curl -sL https://rpm.nodesource.com/setup_10.x | bash -
    

    NodeJS 8.x

    curl -sL https://rpm.nodesource.com/setup_8.x | bash -
    

    NodeJS 6.x

    curl -sL https://rpm.nodesource.com/setup_6.x | bash -
    

    Install node.js with

    yum -y install nodejs
    

    For npm to install native modules, install dev tools

    yum install gcc-c++ make
    

    Or

    yum groupinstall 'Development Tools'
    

    See Node.Js