node.js - 如何设置 Apache ProxyPass 以保留 Express 路由

标签 node.js apache express httpd.conf

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

<IfModule mod_proxy.c>

  ProxyRequests Off

  ProxyPass /node http://localhost:3000/

</IfModule>

Express 应用如下所示:

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

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

我在 CentOS 上运行 Apache 2.4.6。

最佳答案

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

ProxyPass /node http://localhost:3000

关于node.js - 如何设置 Apache ProxyPass 以保留 Express 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33152648/

相关文章:

http - Chrome 浏览器未将 if-modified-since header 发送到服务器

apache - 使用Ab对多个URL进行基准测试

javascript - 在运行 apache 的 blueonyx 服务器上找不到文件

javascript - Express.js - 如何使用 res.render 将警报从 Express 发送到客户端?不重发

javascript - NodeJS 从网络套接字写入中获取响应

javascript - Mongoose:更新文档中的数组不起作用

node.js - 带有 Angular 通用的 web.config

java - 何时为 apache httpclient 调用 ServiceUnavailableRetryStrategy

node.js - 使用 NodeJS/Express 进行正确的错误处理

javascript - 用 cookie 或 localstorage 替换 sails.js redis session 存储