
Create HTTP Server using express.js
Express is a minimal and flexible NodeJS web application framework that provides a robust set of features for we ad mobile applications.
Globally many framework based on ExpressJS Such as :
How to create http server using ExpressJS ?
The following example uses express to create a HTTP Server listening on port 3000, you can use any port
which you want.
make sure you have already installed nodejs in your system to run this example.
First create a folder to keep all files seperate from others
e.g i created a folder myapp, go into this and install express framework using this command
npm install --save express
NOTE: I suppose you have alreayd initiated node application that's why not showing all first few steps for fresh application initialization.
create a new javascript file containing following code (let's name it index.js for example)
index.js
// Import the top-level function of express const express = require('express'); // Creates an Express application using the top-level function const app = express(); // Define port number as 3000 const port = 3000; // Routes HTTP GET requests to the specified path "/" app.get('/', function(request, response) { response.send('Hello, World!'); }); // Make the app listen on port 3000 app.listen(port, function() { console.log('Server listening on http://localhost:' + port); });
from the command line run the following command:
node index.js
Open your browser and navigate to http://localhost:3000 or http://127.0.0.1:3000 to see the response.