NodeJS路由问题

时间:2016-04-06 05:48:32

标签: javascript node.js

如何将下面的url路由到node.js中的不同视图

http://admin.localhost:3000/users/customer/view

http://localhost:3000/users/customer/view

目前它转到我为

设置的相同路线
  

http://localhost:3000/users/customer/view

App.js

....

var users = require('./routes/users');
app.use('/users', users);

....

Users.js

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

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.get('/Customer/Create', function(req, res, next) {
  res.render('customer', {});
});

router.get('/Customer/View', function(req, res, next) {
  res.render('customer', {});
});

router.get('/Employee/Create', function(req, res, next) {
  res.render('customer', {});
});

router.get('/Employee/View', function(req, res, next) {
  res.render('customer', {});
});

module.exports = router;

通过在url admin.yoururl.com之前添加管理员,使用您的网址执行此类操作的术语是什么?

1 个答案:

答案 0 :(得分:1)

由于您使用的是express,因此您可以使用快速中间件express-subdomain

该软件包甚至支持多级子域,如v1.api.domain.com。

您需要为每个子域创建一个路由器,然后使用该包将该路由器绑定到您的快速应用:

var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
var router = express.Router();

//api specific routes 
router.get('/', function(req, res) {
  res.send('Welcome to our API!');
});

router.get('/users', function(req, res) {
  res.json([
    { name: "Brian" }
  ]);
});

app.use(subdomain('api', router));
app.listen(3000);