You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express路由Hello World无响应,请求无日志、返回404/403问题求助

Troubleshooting "Hello World" Route Issues in Express

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 test directory: If you were relying on express.static to serve files, Express couldn’t find a test folder/file in your static directory, so it returned 404. Since your /test route wasn’t properly configured, it never got triggered.
  • 403 with the test directory: When you created the test folder, express.static tried to serve it—but folders require an index file (like index.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

  1. Test the minimal route example locally first to confirm Express works as expected.
  2. Move any express.static middleware after your routes to avoid overriding them.
  3. Fix your reverse proxy config to forward requests to your Express server.
  4. Skip the test directory entirely—you don’t need it for a simple route response.

内容的提问来源于stack exchange,提问作者Ben Muircroft

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:49:25