Jump to content

Express.js vs Node.js Programming Challenge: Routing Dilemma

Featured Replies

I am developing a web application using Node.js and Express.js. However, encounter a challenge with routing that involves choosing between using the core Node.js http module for routing or leveraging Express.js for the same purpose.

Consider the following code snippet:

const http = require('http');
const url = require('url');

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

    if (path === '/home') {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Welcome to the home page!');
    } else if (path === '/about') {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Learn more about us!');
    } else {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        res.end('Page not found!');
    }
});

const port = 3000;
server.listen(port, () => {
    console.log(`Server listening on port ${port}`);
});

I'm also looking at multiple articles to discover the solution to the issue of identifying the benefits and drawbacks of utilizing this simple Node.js http module technique for routing against Express.js. Furthermore, propose a concise solution to improve the code depending on the chosen routing technique. Thank you for delving into this Express.js vs Node.js programming challenge!

You "forgot" to describe what you are asking and what you are struggling with..

 

 

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.