Node js HTTP Modules

Node js HTTP Modules

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows developers to run JavaScript on the server side. One of the core modules in Node.js is the HTTP module, which allows you to create a server and handle HTTP requests and responses.

To use the HTTP module, you first need to import it into your Node.js application by using the require function. For example:

Once you have the HTTP module imported, you can create a server by calling the createServer method and passing in a callback function that will handle the requests. The callback function takes in two arguments: a request object and a response object.

Here's an example of a simple server that listens on port 3000 and sends a "Hello, World!" message as the response:

You can also handle different types of HTTP requests, such as GET and POST, by using the req.method property. Here's an example of how you can handle a GET request and return a JSON object as the response

In summary, the HTTP module in Node.js is a powerful tool that allows you to create a server, handle HTTP requests, and send back responses in a variety of formats. With the help of examples above you can easily understand the basic of Node js HTTP Modules.