i have windows 10 wiht subsystem Linux (Ubuntu-20.04) and I am using WSL2. When I init frontend project (vue project) and I run npm run serve then everything works fine. The application ran on localhost:8080 as i expect. But when i created backend for this app (new project with nodejs, express) I started simple server and in terminal looks fine but when i wanted to go in browser to localhost:5000 to see basic Hello World, the page is loading for a while and then says the webpage localhost didnt send any data.
Here is my app.ts
import express, { Application, Request, Response, NextFunction } from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import 'reflect-metadata'
const app: Application = express()
const port = 5000
app.use(cors)
app.use(bodyParser.json({ limit: '20480kb' }))
app.get('/', (req, res) => {
res.send('Hello!')
})
const server = app.listen(port, 'localhost', () => console.log(`The server is running on port ${port}.`))
Here is my package.json scripts (compile typescript to javascript works fine):
"scripts": {
"start": "nodemon dist/app.js",
"build": "rm -rf dist/ && tsc -w",
"commit": "npx git-cz"
}
Here is my backend terminal, everything here works fine:
https://i.sstatic.net/pJIb9.png
I tried to use netstat to see connection:
https://i.sstatic.net/n5naM.png
My problem is that i cant reach the server on web browser, please help.
if you define explicitly the PORT works, i have the same problem trying to run a basic node - express app on WSL2. basically is a network problem so defining the PORT and HOST solve the issue for me. Here is the code.
const express = require('express');
const app = express();
const PORT = 3000;
const HOST = '0.0.0.0';
app.get('/', (req, res) => res.send('hello world'));
app.listen(PORT, HOST, () => console.log('server running'));
doing that when i put localhost:3000 on my browser it works.
For some reason, when i updated docker desktop this method failed. Another valid solution is open the port in windows. in powershell with admin privileges:
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=192.168.x.x
in 'connectaddress' you must put your IP (wsl2) you can obtain this with ifconfig
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With