Search code examples
google-chrome-oschromebookcrostini

Crostini - Node server doesn't work on localhost


I have a Pixelbook on Chrome 79. In my terminal (Crostini), I run a simple Express app:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Hello!');
});

const PORT = 8080;

app.listen(PORT, () => console.log(`Listening on port ${PORT}...`));

And try it in the browser at localhost:8080 and I get the localhost refused to connect error.

If I use the Angular CLI tool to make a boilerplate Angular project and use ng serve, it tells me it's listening at localhost:4200 - which I try and it does work.

What's the difference? How do I get my app to work on localhost?

I feel like I had this working the last time I tried it a couple months ago and now it's just not working and I can't tell why. I've tried using other ports and restarting my computer and nothing changes.

Edit: I've seen Google demo that the port forwarding should work automatically here: https://youtu.be/pRlh8LX4kQI?t=1160 - but it doesn't seem like it is for me.

Edit 2: If I find the IP of my container with ip addr show | grep inet (for me it was 100.115.92.199) and try that at port 8080 it works. Also, I found someone on Reddit reporting the same issue (link). So I think the automatic port forwarding is broken.


Solution

  • I also have a Pixelbook on Chrome 79. Your code will work on Firefox. To make it work on the Chrome browser you have to pass the host parameter to app.listen().

    const express = require('express');
    const app = express();
    
    app.get('/', (req, res) => {
        res.send('Hello!');
    });
    
    const PORT = 8080;
    const HOST = 'localhost';
    
    app.listen(PORT, HOST, () => console.log(`Listening on port ${PORT}...`));