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
- Web-Socket in NodeJS
- Nodejs | Jimp
- Nodejs | DNS | setServers()
- Nodejs | Web Crawling using Cheerio
- Difference between NodeJS and AngularJS
- Encrypting Data in NodeJS
- Nodejs | GM sharpen() Function
- Dockerizing a simple Nodejs app
- Nodejs | GM charcoal() Function
- Nodejs | GM blur() Function
- Cowsay in Nodejs using Requests library
- NodeJs - Handling invalid routes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.