How to Use Express in Node JS?

17-Aug-2022

.

Admin

How to Use Express in Node JS?

Hi friends,

Here, I will show you node js example express. you can understand the concept of node js express js example. This post will give you a simple example of how to use express in node js. I explained simply how to use express in node.js.

This example is simply print hello world using express node js. Express is a node js web application framework that provides broad features for building web and mobile applications. It provides mechanisms to Write handlers for requests with different HTTP verbs at different URL paths (routes). Express js is a framework based on node js.

let's see below simple example with output:

Step 1: Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Step 2: Installing Express

This step is install the Express framework globally using NPM so that it can be used to create a web application using node terminal.

npm install express --save

Step 3: Create server.js File

server.js

var express = require('express');

var app = express();

app.get('/', function (req, res) {

res.send('Hello Welcome to Nicesnippets.com');

})

var server = app.listen(8081, function () {

var host = server.address().address

var port = server.address().port

console.log("Example app listening at http://%s:%s", host, port)

})

Step 4: Run Node JS App

Open your terminal run it with the following command.

node server.js

Open Your browser and use this URL:

http://localhost:8081/

You will see the following output:

I hope it can help you...

#Node.js Express

#Node JS