简体   繁体   English

Express似乎正在忽略根路由控制器

[英]Express appears to be ignoring controller for root route

I am attempting to send all GET requests on my site (other than API requests) to a controller which checks for the presence of a signed-in user before showing the page. 我试图将网站上的所有GET请求(API请求除外)发送给控制器,该控制器在显示页面之前检查是否存在登录用户。 Otherwise, if not currently signed in, the user is sent to the sign-in page. 否则,如果当前尚未登录,则会将用户发送到登录页面。

The below structure works perfectly for everything but the root route ( '/' ). 下面的结构对除根路由( '/' )以外的所有内容都适用。 For example, if I try to hit /admin or /games as a non signed-in user I am properly sent to the sign-in page. 例如,如果我尝试以非登录用户身份访问/admin/games ,则会正确地发送至登录页面。 Likewise, if I am signed in each page shows properly (note: I am using AngularJS client-side). 同样,如果我在每个页面上签名都正确显示(请注意:我正在使用AngularJS客户端)。 The one issue I have is that when I hit my root route the app attempts to show index.html without ever checking if the user is signed in. I'm confused as to why that route is handled differently, and why my pagesController is seemingly never invoked for this route. 我遇到的一个问题是,当我打了我的根路由时,该应用程序尝试显示index.html而不检查用户是否已登录。我对于为什么以不同的方式处理该路由以及为什么我的pagesController似乎感到困惑从未为此路线调用过。 I've done some 'console logging' and confirmed that pagesController.index is called for each non-API route but '/' . 我已经做了一些“控制台记录”,并确认为每个非API路由(但为'/'都调用了pagesController.index

routes.js routes.js

'use strict';

var controllers = require('./controllers');
var v1Router = require('./routes/api/v1');

var pagesController = controllers.pagesController;

module.exports = function(app) {

  app.use('/api', v1Router);

  app.get('/logout', function(req, res) {
    req.logout();
    res.redirect('/');
  });

  app.get(['*'],
    pagesController.index
  );
};

pages.js pages.js

'use strict';

var path = require('path');

var index = function(req, res) {
  if (req.user) {
    res.sendFile(path.join(__dirname, '../assets', 'index.html'));
  } else {
    res.sendFile(path.join(__dirname, '../assets', 'signin.html'));
  }
};

module.exports = {
  index: index
};

Update: I changed my root from / to /a in my client-side routing and everything works perfectly. 更新:我在客户端路由中将根目录从/更改为/a ,一切正常。 For whatever reason / is not being run through pagesController.index . 无论出于什么原因, /都不通过pagesController.index运行。

Update 2: So this line in my app.js is causing the issue: 更新2:所以我的app.js这一行引起了问题:

app.use(express.static(path.join(__dirname, 'assets')));

With that line removed pagesController.index is properly called on / , however none of my static assets are served so simply removing that line does not solve it. 删除该行后,将在/上正确调用pagesController.index ,但是我的静态资产均未得到提供,因此仅删除该行不会解决该问题。 How can I serve my static assets properly while ignoring index.html ? 如何在忽略index.html同时正确提供我的静态资产?

You can also handle the route in your conditional statements: 您还可以在条件语句中处理路线:

'use strict';

var path = require('path');

var index = function(req, res) {
   if (req.user) {
     res.sendFile(path.join(__dirname, '../assets', 'index.html'));
   } if (req.url === '/') {
     ** Handle Case Here **
   }
  else {
     res.sendFile(path.join(__dirname, '../assets', 'signin.html'));
   }
 };

 module.exports = {
   index: index
 };

Ok, I did some research: A great article and possible solution here:(not sure if it fits your use case) 好的,我做了一些研究:一篇很棒的文章和可能的解决方案:(不确定是否适合您的用例)

http://evanhahn.com/express-dot-static-deep-dive/ http://evanhahn.com/express-dot-static-deep-dive/

app.use(express.static(myStaticPath, {
  index: ['jokes.txt', 'index.html']
})

You can simply give an array of files to serve as index, if the first is found it is served, if not, the second is served, and so on. 您可以简单地给一个文件数组作为索引,如果找到第一个文件,则为它提供服务;如果找不到,则第二个文件为服务,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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