仅显示到一条路线的路线

时间:2017-03-03 21:38:53

标签: node.js express

我使用此中间件路由不同的子域:https://github.com/bmullan91/express-subdomain

问题是,当我拨打的电话不同于' /'它每次调用路线' /'。这是一个日志,我认为更好地解释了我想说的话:

-- This is the log for the '/' route

  express:router dispatching GET / +833ms
  express:router query  : / +3ms
  express:router expressInit  : / +2ms
  express:router logger  : / +1ms
  express:router jsonParser  : / +5ms
  body-parser:json skip empty body +2ms
  express:router urlencodedParser  : / +0ms
  body-parser:urlencoded skip empty body +1ms
  express:router cookieParser  : / +0ms
  express:router serveStatic  : / +2ms
  send stat "/home/degva/git/PP/pg_university/csu/public/index.html" +4ms
  express:router session  : / +6ms
  express-session fetching DWUp......n1W8-smm6Eslm +8ms
  express-session no session found +3ms
  express:router initialize  : / +4ms
  express:router authenticate  : / +5ms
  express:router trim prefix (/) from url / +2ms
  express:router router / : / +1ms
  express:router dispatching GET / +1ms
  express:router <anonymous>  : / +1ms
  express:router <anonymous>  : / +1ms
  express:router <anonymous>  : / +1ms
  express:router <anonymous>  : / +1ms
  express:router dispatching GET / +1ms
  express:view lookup "index.pug" +492ms
  express:view stat "/home/degva/git/PP/pg_university/csu/views/index.pug" +0ms
  express:view render "/home/degva/git/PP/pg_university/csu/views/index.pug" +1ms
  express-session saving anuPe0LUT2et7_qg3qag8shksC7uuZWG +423ms
  express-session split response +1ms
  express-session set-cookie connect.sid=s....
  morgan log request +19ms
GET / 200 967.374 ms - 1088

-- This is the log for the '/p-main/home' route, which ends up rendering '/'

  express:router dispatching GET /p-main/home +138ms
  express:router query  : /p-main/home +0ms
  express:router expressInit  : /p-main/home +9ms
  express:router logger  : /p-main/home +0ms
  express:router jsonParser  : /p-main/home +1ms
  body-parser:json skip empty body +0ms
  express:router urlencodedParser  : /p-main/home +1ms
  body-parser:urlencoded skip empty body +0ms
  express:router cookieParser  : /p-main/home +1ms
  express:router serveStatic  : /p-main/home +0ms
  send stat "/home/degva/git/PP/pg_university/csu/public/p-main/home" +1ms
  express:router session  : /p-main/home +2ms
  express-session fetching anu.......WG +2ms
  express-session session found +1ms
  express:router initialize  : /p-main/home +1ms
  express:router authenticate  : /p-main/home +3ms
  express:router trim prefix (/p-main/home) from url /p-main/home +0ms
  express:router router /p-main/home : /p-main/home +1ms
  express:router dispatching GET / +1ms
  express:router <anonymous>  : /p-main/home +0ms
  express:router <anonymous>  : /p-main/home +1ms
  express:router <anonymous>  : /p-main/home +2ms
  express:router <anonymous>  : /p-main/home +1ms
  express:router dispatching GET / +0ms
  express:view lookup "index.pug" +3ms
  express:view stat "/home/degva/git/PP/pg_university/csu/views/index.pug" +0ms
  express:view render "/home/degva/git/PP/pg_university/csu/views/index.pug" +2m
  express-session touching +239ms
  express-session touched +3ms
  morgan log request +0ms
GET /p-main/home 304 262.937 ms - - <- This gets a 304 since it ends up rendering the same file.

很抱歉,如果我使用了错误的技术词汇。

编辑:这是所请求代码的一部分:

// index.js
/*
This file is being called by app.js that is in the main app directory.
It's being called in this way:

app = express()
require('./app/index.js')(app);
*/

var express = require('express');
var subdomain = require('express-subdomain');

var api = require('./api.js');
var main = require('./main.js');

module.exports = function(app) {
  var router = express.Router();

  router.use(subdomain('api', api));
  router.use(subdomain('www', main));

  app.use('*', router);
}

// main.js
var express = require('express');
var main = express.Router()

main.get('/', function(req, res, next) {
  res.render('index', { title: 'Main' });
});

main.get('/p-main/:name', function(req, res, next) {
  var partial = req.params.name;
  res.render('partials/' + partial);
});

module.exports = main;

上面的日志是被要求的: www.test.dev/www.test.dev/p-main/home

谢谢!

1 个答案:

答案 0 :(得分:0)

我错误地把它放在最后:

app.use('*', router);

意味着被调用的所有其他内容将再次移动到路由器。 相反应该是:

app.use(router);