Since we will use some WebGIS a bit later, let's make a web Hello world too. For this, a new project should be created using npm. Once ready, we will need an additional HTTP module to create a web server. This is a default Node.js module, so we just need to require it in our code:
const http = require('http');
const server = http.createServer((req, res) => {
console.warn('Processing request...');
res.end('Hello world!');
});
const port = 8080;
server.listen(port, () => {
console.warn('Server listening on http://localhost:%s', port);
});
In the preceding code, we bring in the HTTP module via a require method and assign it to a variable. Next, a server is created - whenever it receives a request it logs a message to the console and replies with Hello world!. The final step is making the server listen on port 8080.
When you now launch the script via node index.js, you should see similar output in the console:
Server listening on http://localhost:8080
Whenever you navigate to http://localhost:8080, you should see a Hello world! message in your browser and the following in the console:
Processing request...