Computer scienceBackendNode.jsCore ConceptsInternal modulesHTTP serverhttp module

Routing

Homepage routing example

Report a typo

Complete the code snippet by adding the route to make a request to the home page. Replace the question mark (?) with the correct route.

const http = require('http');

const server = http.createServer((req, res) => {
    const url = req.url;

    if ( url === '?') {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('Welcome to the Home Page!');

    } else {
        res.writeHead(404, { 'Content-Type': 'text/plain' });
        res.end('404 Not Found');
    }
});

const port = 3000;

server.listen(port, () => {
    console.log(`Server is running at http://localhost:${port}`);
});
Enter a short text
___

Create a free account to access the full topic