Node.js – Lesson 5: Create Web-server application

First create file app.js:

const express = require("express");
const app = express();
const port = 8000;

app.listen(port, () => {
    console.log("Server work on "+port); 
});

In this code we require Express framework that provides a robust set of features for web and mobile applications. Define class app – our main application class and port – default server port.

We can start our server using command:

npm run start

But now our server can not do nothing, ot only write in console text: “Server work on port 8000“. Let’s create routing and say our server how to work with Url and write text to Web-browser.

First create folder “routes” and file “routes/index.js” with code:

const mainRoutes = require('./main');

module.exports = function(app) {
    mainRoutes(app);
}

Now create file “routes/main.js“:

module.exports = function(app) {
    app.get('/', (req, res) => {
        res.end('main');
    });
}

And the last – in file “app.js” require our routes – before app.listen insert this command: require(“./routes”)(app);

const express = require("express");
const app = express();
const port = 8000;

require("./routes")(app); // require /routes/index.js 

app.listen(port, () => {
    console.log("Server work on "+port); 
});

Start program in development mode:

npm run dev

Run Web-browser and open http://localhost:8000:

Similarly you can create /about, /contact or any other route on your NodeJS Web-site.

Node.js – Lesson 4: Deploy application on server using git, configure Nginx and pm2

To deploy Node.js application in Internet we need server and domain name. We will use Ubuntu 18.04. On server install Node.js using commands:

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install nodejs

Also we need to install Nginx web server to proxy http and https requests (from 80, 443 ports) to node application port.

sudo apt-get install nginx

We need to install Git if we want to deploy our application from git repository:

sudo apt-get install git

Next go to /home directory and use command:

git clone repositury_url

This command will clone all files from your git repository to server.

Next we need to install all dependencies from package.json:

npm install

And now we can run our application using command:

node index.js

But this command will stop work if our server will restart or if we close SSH connection to server. To run our application as daemon on server startm use special command pm2. Install it globally with Npm package manager:

sudo npm install pm2 -g

This program will run our node.js application in background:

pm2 start index.js

TO automatic start pm2 on system boot – generate startup script using command:

pm2 startup

And save configuration using command:

pm2 save

Node.js – Lesson 2: Start server, install dependencies

Next step of creating Node.js application is installing additional frameworks and dependencies that facilitate work with the project.

In our lesson we will use popular web framework “Express” and utility “Nodemon”, that will monitor for any changes in your source and automatically restart your server.

To install this dependencies in console write commands:

npm install express
npm install -D nodemon

Flag -D means that this is developer dependency and it not used in production project.

In section “scripts” write create script “start” and “dev”:

"start": "node index.js",
"dev": "nodemon index.js"

Node.js – Lesson 1: Create first application

First of all You need to install Node.js. On Windows computers go to https://nodejs.org/en/ website, download latest version and install it.

On Linux (Debian, Ubuntu Mint) add PPA repository of Node.js:

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

And install nodejs package

sudo apt-get install nodejs

To check Node.js version use command:

node -v
v12.18.3

Now let’s create new application. We will use Visual Studio Code editor. Open terminal and initialize NPM by command:

npm init

This command will create package.json configuration file. This file contains information about all packages, used in application.