简体   繁体   中英

NodeJS Routing issue

how can I route below url to different view in node.js

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

and

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

currently it go to the same route that I set for

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;

and what is the terminology for doing something like this with your url by adding admin before url admin.yoururl.com ?

Since you are using express, you can use the express middleware express-subdomain .

The package even supports multi level subdomains like v1.api.domain.com.

You need to create one Router per subdomain and then bind that Router to your express app using the package:

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM