Setup NGINX with Nodejs - PART 1

Setup NGINX with Nodejs - PART 1

Setup NGINX and Nodejs with SSL connection

NGINX is a high-performance HTTP server as well as a reverse proxy . Unlike traditional servers, NGINX follows an event-driven, asynchronous architecture. As a result, the memory footprint is low and performance is high. If you’re running a Node.js-based web app, you should seriously consider using NGINX as a reverse proxy.

Note: Node also has a built-in HTTPS module and can be configured to read the necessary certificate files without the need for a reverse proxy.

Reference: How to Use SSL/TLS with Node.js.


Setup a Node.js Server

First, let’s create a simple Node.js server/api. We will start by installing the Express package within a project,

mkdir node-express && cd node-express
npm init
npm install express

Finally your package.json will look like as mentioned below,

{
  "name": "node-express",
  "version": "1.0.0",
  "description": "A Node.js RESTful API Tutorial Project (Build a simple shop API)",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon server.js"
  },
  "keywords": [
    "node",
    "restful",
    "api"
  ],
  "author": "sriram.hashnode.dev",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.2"
  },
  "devDependencies": {
    "nodemon": "^1.12.1"
  }
}

Create a file called server.js, with the following code

const http = require('http');
const app = require('./app');
const port = process.env.PORT || 3000;
const server = http.createServer(app);
server.listen(port);

Now define a REST API for sample request then have them in the file called api.js, with the following code

const express = require("express");
const app = express();
const getRoutes = require("./api/routes/getdata");

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  );
  if (req.method === 'OPTIONS') {
      res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
      return res.status(200).json({});
  }
  next();
});

// Routes which should handle requests
app.use("/", getRoutes);

app.use((req, res, next) => {
  const error = new Error("Not found");
  error.status = 404;
  next(error);
});

app.use((error, req, res, next) => {
  res.status(error.status || 500);
  res.json({
    error: {
      message: error.message
    }
  });
});

module.exports = app;

You can start the node server with the command npm run start. After all the command execution of initial stage.


Setting Up and Configuring NGINX

Now let’s open up the NGINX default site config file. If you want to create your own config file, just open your favourite code editor and have you configurations one by one. VS Code is my favourite code editor

Your NGINX's server configuration will look like as mentioned below,

worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
/****** Your configuring setup will go here ******/
}

Finally let's have the further discussion/explanation in upcoming Setup NGINX with Nodejs - PART 2