What is Node.js ?
Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP). The HTTP module can create an HTTP server that listens to server ports and gives a response back to the client.
The function passed in the http.createServer() will be executed when the client goes to the url http://localhost:8081.
Steps to run the code:
- Save the above code in a file with .js extension
- Open the command prompt and goes to the folder where the file is there using cd command.
- Run the command node file_name.js
- Open the browser and go the url http://localhost:8081
When http://localhost:8081 is opened in browser.
The http.createServer() method includes request object that can be used to get information about the current HTTP request e.g. url, request header, and data.
The following example demonstrates handling HTTP request and response in Node.js.
In the above example, req.url is used to check the url of the current request and based on that it sends the response.
Command to Run code:
- URL: localhost:3000
- URL: localhost:3000/webtech
- URL: localhost:3000/DS
- URL: localhost:3000/algo
- Nodejs | Automatic restart NodeJs server with nodemon
- Difference Between Web server and Application server
- Nodejs | DNS
- Nodejs | Jimp
- Nodejs | DNS | setServers()
- Web-Socket in NodeJS
- Web Server and Its Type
- Nodejs | GM paint() Function
- Nodejs | GM drawRectangle() Function
- Nodejs | GM drawPolygon() Function
- Nodejs | GM roll() Function
- Nodejs | GM raise() Function
- Nodejs | GM segment() Function
- Nodejs | GM orderedDither() Function
- Nodejs | GM quality() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.