Search code examples
javascriptexpressdnslisten

How to make ExpressJS listen on a domain


How do I make ExpressJS listen on domain, I recently got a domain and I want Express.JS to listen on that domain without any ports. How do I do that. If express can't do that could you tell me a package that do it? Thanks


Solution

  • ...and I want Express.JS to listen on that domain without any ports.

    You always have to listen on a port. The default port for HTTP (for instance, when you go to http://example.com) is port 80 (for HTTPS it's 443). Those are the standard ports web servers listen on.

    So assuming you're running your ExpressJS code on a server at the IP address that the domain name resolves to, you just use the standard ExpressJS code for listening for HTTP requests. Here's their "Hello World" example, modified to use port 80 (and semicolons):

    const express = require('express');
    const app = express();
    const port = 80; // <===
    
    app.get('/', (req, res) => res.send('Hello World!'));
    
    app.listen(port, () => console.log(`Example app listening on port ${port}!`));