繁体   English   中英

无法使用 ngnix 代理通行证访问快速子路由

[英]can't access express sub routes on with ngnix proxy pass

我正在将一个 MERN 堆栈应用程序部署到 ec2,但遇到了无法访问子路由的问题。

服务器.js

const app = express();

app.use(bodyParser.json())

app.use(express.json())
app.use(cors());
app.options('*', cors()); 


app.use((err, req, res, next) => {
res.status(400).send({httpStatus:400, response: "Invalid Request. Only valid json requests accepted."})
});

app.get('/api',(req,res) => res.send("hello world"))
app.post('/api/users/create',(req,res) => console.log("creating user..."))
app.listen(5000,'localhost',() => console.log('listening on 5000'))

nginx配置文件:

server {
listen 80;
 #server_name your_domain.com;
  location / {
      # This would be the directory where your React app's static files are stored at
       root /my-app/client/build/;
       try_files $uri /index.html;
  }
  location /api/ {

   proxy_pass http://localhost:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

 }

}

静态文件和单个路由工作,但带有子路由的路由不起作用..请求超时。 有什么帮助吗?

你永远不会结束请求,这就是它超时的原因。

app.post('/api/users/create',(req,res) => {
  console.log("creating user...");
  res.send('created');
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM