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
- Difference between Apache Tomcat server and Apache web server
- Difference between Web Browser and Web Server
- How to choose Web Hosting Server and Web Domain ?
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- Nodejs | Web Crawling using Cheerio
- Web-Socket in NodeJS
- Which One is Most Demanding Back-End Web Framework between Laravel , NodeJS and Django ?
- Create a web server and run PHP script on it using Raspberry Pi
- Build a Simple static file web server in Node
- Web Server and Its Type
- Difference between Web Scraping and Web Crawling
- Difference between Web Designer and Web Developer
- Differences between Web Services and Web API
- Progressive Web App - A Combination of Native and Web App
- 10 Web Development and Web Design Facts That You Should Know
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
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.