Dynamic URL handling example

Report a typo

Arrange the code blocks to create a dynamic route in Node.js. It should extract the post ID from the URL and respond with "You are reading post #456" when accessed with "/posts/456".

Put the items in the correct order
const http = require('http');
server.listen(port, ()=>console.log(`Listening on port ${port}`));
const port = process.env.PORT || 8080;
const server = http.createServer((request, response) => {
    if (request.url.startsWith('/posts/')) {
        const postId = request.url.split('/')[2];
        response.writeHead(200, { 'Content-Type': 'text/plain' });
        response.write(`You are reading post #${postId}`);
    
    response.end();
}});

Create a free account to access the full topic