Setup the Node JS application

  1. Install Node JS from https://nodejs.org/en/
  2. Create a directory and inside that directory give the command npm init.
  3. Install express by npm install express
  4. Create sub-directory src where we going to put all our code.
  5. Create a file app.js

Code app.js

const express = require(“express”);

const app = express();

app.get(“”, (req, res) => {

res.send(“Hello Express”);

});

app.listen(3000, () => {

console.log(“Server is up on port 3000.”);

});

Run the command node src/app.js

How to set up nodemon?

Nodemon allows us to re-run the server if any modification made on code level no need again restart the server by node src/app.js

  1. Install nodemon inside the project npm install nodemon
  2. Inside script of package.json add “dev”: “nodemon src/app.js”
  3. Run the project by using npm run dev

GitHub: https://github.com/msunil037/Webserver-NODEJS


Setup MongoDB

  1. Install mongodb to use the mongodb driver (npm install mongodb)

Initial Code:-

const mongodb = require(‘mongodb’);

const MongoClient = mongodb.MongoClient;

const connectionURL = ‘mongodb://127.0.0.1:27017’;

const databaseName = ‘test’;

MongoClient.connect(connectionURL, { useNewUrlParser : true }, (error, client) => {

if(error){return console.log(‘Unable to connect to database’);}

console.log(‘Connected Successfully!’);

const db = client.db(databaseName);

})

Inserting a single document

db.collection(“users”).insertOne(

{name: “Sunil”,age: 27},

(error, result) => {

if (error) return console.log(“Unable to insert user”);

console.log(result.ops);

});

Inserting a multiple document

db.collection(‘users’).insertMany([

{name: “Sunil”,age: 27},

{name: “Sibun”,age: 25}

], (error, result) => {

if(error) return console.log(“Unable to insert user”);

console.log(result.ops);

})

What is…

Sunil Majhi

Full Stack Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store