Creating a simple HTTPserver using Node.js is relatively straightforward. You can use the built-in
http module to achieve this. Here's a step-by-step guide on how to create a basic HTTP server in Node.js:
Import the http Module: First, you need to import the
http module, which comes bundled with Node.js.
const http = require('http');
Create an HTTP Server: Next, create an HTTP server using the
http.createServer() method. You'll need to provide a callback function that will be called whenever a request is received.
const server = http.createServer((req, res) => {
// Request handling code goes here
});
Define Request Handling Logic: Inside the callback function, you can define the logic for handling incoming HTTP requests (req) and sending responses (res). For a simple example, you can send a "Hello, World!" response.
Specify the Listening Port and Host: To make the server listen for incoming requests, specify the port number and, optionally, the host (e.g., 'localhost').
const port = 3000;
const host = 'localhost';
server.listen(port, host, () => {
console.log(`Server is running at http://${host}:${port}/`);
});
Start the Server: Finally, call the server.listen() method to start the HTTP server and make it listen on the specified port and host.
Testing the Server: You can open a web browser and visit
http://localhost:3000/ (or the host and port you specified) to see your "Hello, World!" response. You can also use tools like
curl or Postman to test the server from the command line.
Here's the complete code for a simple HTTP server:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World!\n');
});
const port = 3000;
const host = 'localhost';
server.listen(port, host, () => {
console.log(`Server is running at http://${host}:${port}/`);
});
This code creates a basic HTTP server that responds with "Hello, World!" to any incoming requests. You can expand upon this foundation to build more complex web applications and APIs in Node.js.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Creating a simple HTTP server using Node.js is relatively straightforward. You can use the built-in http module to achieve this. Here's a step-by-step guide on how to create a basic HTTP server in Node.js:
Import the http Module: First, you need to import the http module, which comes bundled with Node.js.
Create an HTTP Server: Next, create an HTTP server using the http.createServer() method. You'll need to provide a callback function that will be called whenever a request is received.
Define Request Handling Logic: Inside the callback function, you can define the logic for handling incoming HTTP requests (req) and sending responses (res). For a simple example, you can send a "Hello, World!" response.
Specify the Listening Port and Host: To make the server listen for incoming requests, specify the port number and, optionally, the host (e.g., 'localhost').
Start the Server: Finally, call the server.listen() method to start the HTTP server and make it listen on the specified port and host.
Testing the Server: You can open a web browser and visit http://localhost:3000/ (or the host and port you specified) to see your "Hello, World!" response. You can also use tools like curl or Postman to test the server from the command line.
Here's the complete code for a simple HTTP server:
This code creates a basic HTTP server that responds with "Hello, World!" to any incoming requests. You can expand upon this foundation to build more complex web applications and APIs in Node.js.