const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
In this example, the http module is imported, and an HTTP server is created using the http.createServer method. The http.createServer method takes a request event handler function as an argument and the port is defined as 3000, which is called every time a client makes a request to the server.
The request event handler sets the response status code to 200 (OK), sets the content type to plain text, and writes the message "Hello World" to the response using the res.end method.
The server is started using the server.listen method, which takes a port number as an argument and a callback function that is called when the server is ready to receive requests. In this example, the server is listening on port 3000.
This example demonstrates the basic structure of an HTTP server in Node.js. You can modify the request event handler to handle different types of requests and return different types of responses, as needed.
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
if (req.url === '/data') {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ message: 'Hello World' }));
} else {
res.statusCode = 404;
res.end();
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
In this example, the request event handler checks the URL of the incoming request using the req.url property. If the URL is /data, the server returns a JSON response with a message of "Hello World". If the URL is not /data, the server returns a 404 status code.
This example demonstrates how you can use the req.url property to handle different endpoints in an HTTP server. You can add additional endpoints as needed and return different types of responses based on the URL of the incoming request.
const http = require('http');
const os = require('os');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
if (req.url === '/data') {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ message: 'Hello World' }));
} else if(req.url === '/cpu') {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ cpus: os.cpus() }));
} else {
res.statusCode = 404;
res.end();
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
In this example, the os module is imported, and the information about the operating system's CPUs is obtained using the os.cpus method. The information is returned as a JSON response with a key of cpus.
This example demonstrates how you can use the os module to obtain information about the operating system in Node.js. You can modify the code to obtain additional information about the operating system, as needed.