Nodejs在URL中有一个可选的额外路径

时间:2016-06-10 14:30:13

标签: node.js express nginx

我正在开发一个系统的一部分,我们有两个应用程序共享同一个域,因此nginx使exampleurl.com转到一个应用程序而example.com/admin/*转到第二个。

/ admin / *部分将使用express。

进入NodeJs应用程序

是否有一种优雅的方法可以确保节点可以添加/ admin而无需执行

app.get('/admin/endpoint', ...)

3 个答案:

答案 0 :(得分:0)

您可以使用http://expressjs.com/fr/api.html#router

var router = express.Router();

router.get('/', function(req, res) {
  res.send('One page in admin website');  
});

// router.get('/adminWebsite'); // idem for all routes(you always use this router)

app.use('/admin', router);

答案 1 :(得分:0)

你会使用路由器(最好),但你也可以创建另一个快递应用程序。使用不同的应用程序允许您拥有更多的全球中间装置控制,而使用不同的路线意味着它们共享相同的快递实例。

var express = require('express');
var admin = express(); // <- this is now your admin application
var app = express(); // <- this is your main applicaiton regular users


//you will need to set up middlewear like body-parser and stuff for both of them now
//but it allows you to use different logging and authentication system or w.e you
//want

//once everything is done you can 'MOUNT' virtually the admin app to the regular app

app.use('/admin', admin); //<- this will nest apps together but allow the sub-app admin
//to be it's own instance.

答案 2 :(得分:0)

// app.js incrementBy //你管理模块(你可以路由的地方)

const admin = require('admin');

//管理模块的索引文件

app.use('/admin', [functions... example authCheck], admin);

// main.js

`var express     = require('express');
var router      = express.Router();

router.use(function authCheck(req, res, next) {
    //check if user is logged
});

router.get('/main', require('./main').get);

module.exports = router;`

现在您可以访问site.com/admin/main