Ticker

6/recent/ticker-posts

How to Deploy Node.js Application on your Ubuntu Web Server

Node.js is an open-source JavaScript runtime platform that runs JavaScript code on the server side.

Ubuntu Node Server



NodeJS Installation Steps

Update your ubuntu system

# sudo apt-get update

To install node.js

# sudo apt-get install nodejs

To install npm

# sudo apt-get install npm

Check the node and npm version

# nodejs -v

Output
v8.11.3


# npm -v

Output
5.6.0


Step 2 — Creating a Node.js Application

# mkdir nodeproject

# cd nodeproject

Create server.js node application. Below example of server.js file. Enter this code in a server.js file.

# nano server.js

const http = require('http');                                 
const hostname = 'localhost';                             
const port = 4000;                                              
const server = http.createServer((req, res) => {
  res.statusCode = 200;                                     
  res.setHeader('Content-Type', 'text/plain');      
  res.end('Hello World Test!\n');                          
});                                                                       
server.listen(port, hostname, () => {                   
  console.log(`Server running at http://${hostname}:${port}/`);
});                                                                        


Start the node project

# node server.js

Output
Server running at http://localhost:4000/

curl through directly you can check.

# curl http://localhost:4000


Output
Hello World Test!


Kill the application by pressing CTRL+C.

Also, you can node project start via forever and pm2 process. This service uses a background in project process running.


How to pm2 install:-

# sudo npm install pm2 -g

Then start node application

# sudo pm2 start server.js

When the server automatically starts run the following commands:

# sudo pm2 startup
# sudo pm2 save



How to forever install:-


# sudo npm install forever -g

Then start node application

# sudo forever start server.js


Also, node application starts /var/www/html path in you can directly run node application.

Reactions

Post a Comment

1 Comments

  1. Contact IT Networks Technologies if you want to buy Alienware Gaming PCs at affordable prices. It is completely dedicated to gamers. Gamers can now keep playing for long hours without having to worry about low battery, heating issues, etc.

    ReplyDelete
Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)