'Not able to access node.js express endpoints in browser, but postman works just fine
I am working on an express app locally. I have a route in my app that looks like
rootRouter.get('/', async (_: Request, res: Response) => {
res.status(OK).send('Hello!');
});
Command line run
node -r module-alias/register ./dist --env=development
Browser behavior:
I cannot see the route through my browser localhost:6000. I see "This site can’t be reached". It's like that for all URLs. I don't see anything appear in my server logs.
Postman behavior:
When I hit that same route localhost:6000 with a get request in postman, I see the text "Hello!". Logs - GET / 200 0.354 ms - 25
Heroku run
heroku local web -p=5000 (calls the same node -r module-alias/register ./dist --env=development as above, but uses port 5000 instead)
Browser behavior:
I CAN see the route through my browser localhost:5000. I see "Hello!". I see the request in my logs
8:44:45 PM web.1 | GET / 200 0.517 ms - 25
8:44:45 PM web.1 | GET /favicon.ico 200 1.928 ms - 12014
Postman behavior:
Also works, I see "Hello!", logs - 9:01:29 PM web.1 | GET / 200 0.193 ms - 25
Rough server code:
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(cookieParser());
app.use(morgan('dev'));
app.use('/', rootRouter); //contains my route that I posted above
// error handling
app.use((err: Error, _: Request, res: Response, __: NextFunction) => {
const message = err.message;
return res.status(500).json({ error: message });
});
const viewsDir = path.join(__dirname, 'views');
app.set('views', viewsDir);
const staticDir = path.join(__dirname, 'public');
app.use(express.static(staticDir));
Solution 1:[1]
The Problem
I realized what the problem was. Specifically, port 6000 is forbidden by many browsers. https://chromium.googlesource.com/chromium/src.git/+/refs/heads/master/net/base/port_util.cc
It was firefox that helped me figure it out - since their error message is much more specific than chromes.
Firefox's error message:
"This address is restricted This address uses a network port which is normally used for purposes other than Web browsing. Firefox has canceled the request for your protection."
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | A Herbig |
