• Register

Share:- Whatsapp Facebook Facebook

------------------------------------- Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.


0 votes
How to create a web server using node.js?
in Node.js by

1 Answer

0 votes
For creating a web server by using node.js see the following steps:-

Step1 Open command prompt and go to your directory [cd DIRECTORY_NAME] where you want.
Step2 Type command "npm install express" to install express
Step3 Create a file server.js and type following in that file:-

var host = "";
var port = 7070;
var express = require("express");

var app = express();
app.use(express.static(__dirname + "/public")); //static files in ROOT/public folder

app.get("/", function(request, response){ //root directory
    response.send("Server started on " + port + " port");

app.listen(port, host);

Step4 Start server by using following command:-
      * node server.js or nodemon server.js
      (nodemon:- for watch file changes and restart server again and againg when any changes in file)
Step6 Open web browser and type following url in address bar:-
Step7 See on the command prompt "Server started on 7070 port".
by (4.4k points)