Express路由Hello World无响应,请求无日志、返回404/403问题求助
Let’s walk through fixing your problem step by step—this sounds like a mix of routing misconfiguration and potential static file/reverse proxy conflicts.
1. Fix Your Express Route First
The core issue is likely that your /test route isn’t properly defined, or it’s being overridden by static file middleware. Start with this minimal, guaranteed-working example:
const express = require('express'); const app = express(); const port = 10001; // Define the /test route BEFORE any static middleware app.get('/test', (req, res) => { console.log('Incoming request to /test'); // Add this to confirm the route is being hit res.send('Hello World'); }); // Only add static middleware if you need it, and ensure it comes AFTER your routes // app.use(express.static('your-static-folder')); app.listen(port, () => { console.log(`Server running on port ${port}`); });
Run this code, then test locally first: use curl http://localhost:10001/test on your server. You should see "Hello World" in the response, and the log message in your Node console. If this works, the problem lies with your domain/proxy setup; if not, double-check that no other code is overriding this route.
2. Why 404/403 Happened
- 404 without the
testdirectory: If you were relying onexpress.staticto serve files, Express couldn’t find atestfolder/file in your static directory, so it returned 404. Since your/testroute wasn’t properly configured, it never got triggered. - 403 with the
testdirectory: When you created thetestfolder,express.statictried to serve it—but folders require an index file (likeindex.html) to display, or directory listing enabled. Since you didn’t have an index file, and directory listing is disabled by default, Express returned a 403 (forbidden) error. Your route was never hit because static middleware takes priority if defined before your routes.
3. Check Domain & Reverse Proxy Configuration
Since you’re using a custom domain, confirm:
- Your domain’s DNS records point to your server’s public IP.
- Your reverse proxy (like Nginx) is correctly forwarding requests to port 10001. For example, your Nginx config should include:
location /test { proxy_pass http://localhost:10001/test; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }
If the proxy isn’t set up correctly, requests to your domain won’t reach your Express server at all—which explains the missing console logs.
4. Verify Port & Firewall
You mentioned port 10001 is open, but double-check with netstat -tulpn | grep 10001 to confirm your Express server is actually listening on that port. Also, ensure your server’s firewall (like ufw or iptables) allows incoming traffic on 10001.
Quick Recap
- Test the minimal route example locally first to confirm Express works as expected.
- Move any
express.staticmiddleware after your routes to avoid overriding them. - Fix your reverse proxy config to forward requests to your Express server.
- Skip the
testdirectory entirely—you don’t need it for a simple route response.
内容的提问来源于stack exchange,提问作者Ben Muircroft

