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

时间:2020-01-12 00:13:51

标签: node.js express nginx

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

server.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;

 }

}

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

1 个答案:

答案 0 :(得分:0)

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

SELECT DISTINCT Id,
CASE WHEN Id NOT IN (SELECT DISTINCT ParentId FROM NodeTable WHERE ParentId IS NOT NULL ) 
     THEN "Leaf"
      WHEN ParentId IS NULL THEN "Root"
      ELSE "Inner"
      END AS NodeType
FROM NodeTable
ORDER BY Id