const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});
- Import the 'http' module.
- Create an HTTP server using the
createServer
method, which takes a callback function with request (req
) and response (res
) parameters. - Set the HTTP status code of the response to 200.
- Set the 'Content-Type' header of the response to 'text/plain'.
- Send the 'Hello, World!\n' string as the response body.
- Create a constant named
PORT
and set its value to 3000. - Make the server listen on the specified port (3000) and log a message to the console when the server is successfully running.