7 Steps To Create Hello World App in NodeJS using ExpressJS Framework

  1. Download and install node.js
  2. Create a folder and CD into that folder using using node.js command prompt
  3. run the command npm init
  4. To install and use expressjs ( Fast, unopinionated, minimalist web framework for Node.js ), run the command npm install express –save
  5. Create a new file and name it server.js (code is attached at the end of this post). Add the code (Server.JS File Code), in this file.
  6. To run the node.js app, run the command node server.js
  7. Visit http://localhost:3000/ to see the results

Extra Packages:

  1. To automatically restart the server, install supervisor package (nodemon can also be used) by running the command npm install npm install supervisor and then run the app using the command supervisor index.js
  2. To get body response from post request, we can use body-parser package. To install it we can use run the command: npm install –save body-parser
  3. To generate json web tokens, we can use jsonwebtoken package. To install it we can run the command: npm install jsonwebtoken –save

Server.JS File Code

var express = require('express');
var app = express(); // creates an express application

//routes
app.get('/', (req, res) => {
    res.send('Hello World');
});

app.listen(3000, function(){
    console.log('Server is running at port : ' + 3000);
});

Leave a Comment

Your email address will not be published. Required fields are marked *