How to install Nodejs in ubuntu

Posted By : Renu Yadav | 31-Jul-2018

Nodejs is open source server environment .By Nodejs you can add , delete and update data in database.  In Nodejs you can create API for get the data from the database and store the data in database. Nodejs API return the json format data. So you can show this data in front page of your project. Nodejs is use for handling the backend of project. Nodejs work as asynchronus. Means request come to the thread and go to the event queue .Event queue send the request to the Thread and thread fetch the data from the database. 

For installing the Nodejs in your desktop. Follow the below steps to install the Nodejs . 

1) Create folder in your drive .

2) Go to the folder in command prompt . 

3) Run the command npm init -y 

4) After run the command JSON file is create in your command prompt . After the ENTER  the button you get some information about the author name , node version , author , description etc . 

5) Run the command npm install express body-parser ejs mongojs --save . This command install the express js , body parser, mongodb. 

6) After install the package check the JSON file. You can see that new packages install in your project and that be define in your JSON file.

package.json

 

 

{
  "name": "mynodeproject",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.18.2",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "express": "~4.15.5",
    "jade": "~1.11.0",
    "morgan": "~1.9.0",
    "serve-favicon": "~2.4.5"
  }
}

After install the package in your project . Go the command prompt and go the your project folder. Create js file in your project and paste the below code in js file.  

server.js

 

 

const express= require('express');

const app = express();

app.listen(process.env.port || 4000,function(){
   console.log("now listening for requests");
});

 Save the code in your file and run file in command prompt. Go to the your project folder in command prompt and run the command node server . You can see the  "now listening for requests" is print on your command prompt. Your Nodejs setup is complete and your file is running on the server.

 

 

 

 

About Author

Author Image
Renu Yadav

Renu is an Associate Consultant -WordPress in Oodles. She likes watching movies and reading books.

Request for Proposal

Name is required

Comment is required

Sending message..