簡體   English   中英

在 express 中覆蓋 app.get('*', func) 路由

[英]Override app.get('*', func) route in express

我有一個與 NodeJS 服務器一起運行的 Angular 應用程序。

我的 server.js 文件中的這段代碼:

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
});

require('./server/routes/routes')(app, passport);

似乎阻止了對我的 api 的任何 GET 請求,例如:

module.exports = function (app, passport) {
  app.get('/api/profile', isLoggedIn, function (req, res) {
      res.status(200).json(req.user);
  });

當導出的 /api/profile 處理程序更改為 post 時,請求有效。

路由不應該覆蓋初始處理程序嗎? 如何實現這一目標? 我可以為除“/api”以外的所有路線提供應用程序嗎?

移動app.get('*')使其成為最后一個聲明的路由:

require('./server/routes/routes')(app, passport);

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
});

Express 按照聲明的順序檢查路由處理程序,而不是按照它們的特異性(它們與特定請求匹配的程度)的順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM