如何设置 Apache ProxyPass 以保留 Express 路由 [英] How to set up Apache ProxyPass to preserve Express routes

查看:31
本文介绍了如何设置 Apache ProxyPass 以保留 Express 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Apache 配置中,我将 /node 上的所有流量转发到端口 3000,其中 Express 服务器正在侦听.

In my Apache config im forwarding all traffic on /node to port 3000, where the Express server is listening.

<IfModule mod_proxy.c>

  ProxyRequests Off

  ProxyPass /node http://localhost:3000/

</IfModule>

Express 应用程序如下所示:

The Express app looks like this:

var express = require('express');

var app = express();
var router = express.Router();

router.route('/route/:id').get(function (req, res) {

    res.json({ description: 'Welcome to a route with an ID' }); 
});

router.route('/route').get(function (req, res) {

        res.json({ description: 'Welcome to the normal route' }); 
});

router.route('/').get(function (req, res) {

    res.json({ data: 'Welcome to the app' }); 
});

app.use('/', router);

app.listen(3000);

当我将浏览器定向到 http://myserver.com/node 时,我得到了响应 { data: 'Welcome to the app' },很好.不过,当我尝试去 http://myserver.com/node/routehttp://myserver.com/node/1210 我收到一个错误 Cannot GET//route.

When I direct my browser to http://myserver.com/node I get the response { data: 'Welcome to the app' }, which is fine. Though, when I try to go http://myserver.com/node/route or http://myserver.com/node/1210 I get an error Cannot GET //route.

有什么想法可以更新我的 Apache 配置以保留 Express 路由吗?

Any ideas how I can update my Apache config to preserve the Express routes?

我在 CentOS 上运行 Apache 2.4.6.

I'm running Apache 2.4.6 on CentOS.

推荐答案

您的主机末尾有一个额外的 /.尝试将其更改为:

You have an extra / at the end of your host. Try changing it to:

ProxyPass /node http://localhost:3000

这篇关于如何设置 Apache ProxyPass 以保留 Express 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆