NodeJs web server | create web server on nodeJs
To access web pages of any web application, you need a web server. The web server will handle all the requests for the web application.
e.g IIS is a web server for ASP.NET, web applications and Apache is a web server for PHP or java web applications.
NodeJs provides capabilities to create your own web server which will handle HTTP requests asynchronously. you can use IIS or Apache to run NodeJS web application but it is recommended to use NOde.js web server.
How to create Web server using NodeJS
- The http.createServer() method includes request and response parameters which is supplied by NodeJS.
- The request object can be used to get information about the current HTTP request.
- e.g. url, request header, and data.
- The response object can be used to send a response for a current HTTP request.
- If the response from the HTTP serer is supposed to be displayed as HTML, you should include an HTTP header with the correct content type:
Outputconst http = require("http");const server = http.createServer((req,res)=>{res.end('hello from the others side')});server.listen(8000,"127.0.0.1",()=>{console.log('listening port no.');});
Then go to brower and type localhost:8000(port no.) in url.
hello from the other side
Post a Comment