【问题标题】:Nodejs - Hosting to subrouteNodejs - 托管到子路由
【发布时间】:2016-07-07 00:41:06
【问题描述】:

我想在 iis 的子路由中托管我的 nodejs 服务器应用程序。我想要做的是将我的应用程序托管为 localhost:3000/node/ 而不是 localhost:3000/。 这可以通过

来实现

改变端点

app.get('/', moduleRoutes.root);
app.post('/auth/signup/', authenticationRoutes.signup);

app.get('/node/', moduleRoutes.root);
app.post('/node/auth/signup/', authenticationRoutes.signup);

但我不想每次更改托管路径时都更改所有 api 端点。

另一个是

app.use((req, res, next) => {
    //change request location from here by changing 
    req.url = req.url.replace('localhost:3000/node/', 'localhost:3000') 
    //somthing like that
    authorization.memberinfo(req, res, next);
}); 

但这看起来不像是实现这一目标的正确方法。请引导我走向正确的方向。谢谢。

【问题讨论】:

    标签: node.js api routing hosting


    【解决方案1】:

    您可以在 /node 上安装一个路由器,然后将所有路由添加到该路由器:

    // These three lines could even be placed in a separate file that you
    // would `require()` and use in your app.js
    var router = express.Router();
    router.get('/', moduleRoutes.root);
    router.post('/auth/signup/', authenticationRoutes.signup);
    
    app.use('/node', router);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2014-10-27
      • 2015-05-12
      相关资源
      最近更新 更多