快速路由——路由到 /secrets 时出现 404 错误,我错过了什么

Express routing -- 404 error when routing to /secrets, what am I missing

我有一个快速应用程序设置,但我不知道如何让它正确路由。目前我在 app.js:

中有这个
var secrets = require('./routes/secrets');
var app = express();

app.use('secrets', secrets);

以下在/routes/secrets.js

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

/* GET 'secrets' */
router.get('/secrets', function(req, res, next) {
  res.render('secrets', { title: 'Colour Hack v1.1' });
});

module.exports = router;

文件 /views/secrets.jade 包含以下代码:

extends layout

block content
  h1 A Very Green Affair
  p It began at a Green Party hustings. I was wearing a beige trenchcoat, set off perfectly by an exquisitely cut Fedora, tailored by the finest vegan outfitters a party councilor's allowance can buy. We locked eyes over the pay-what-you-like FoodCycle buffet and I knew right then something special was about to occur. Amelia Womack wore a pant-suit and has a really nice hair-style. Once she noticed me she approached immediately.
  p "Hi Amy."
  p She said, though it appeared as if the name was somehow unfamiliar to her, and she looked into my eyes with the sort of intensity you might expect of a fawn who's just single-handedly decimated a pack of ravaging wolves and has rounded on the ring-leader who has unexpectedly turned out to be another fawn with particularly big teeth and wolf-like fur who had managed to convince the wolves it was one of them.
  p "It's strange I should run into you as I was just thinking about that... policy suggestion you made."
  p Amelia said the word 'that' with an odd intonation, so it seemed almost as if she were thinking about something other than the policy document. Something secret.
  p "The cat policy," I replied, my words sounding both artful and powerful as I spoke them, and considered her choice of blusher, which seemed a particularly deep red, so that it underscored her already stunning features with a power and authority that lost nothing of femininity. Amelia noticed a kind of damp feeling and wondered if she should have brought a change of underwear but she carried on in a business-like, official sort of way which also seemed to tred a strangely fine line between school-girlish and teacherly.
  p "Yes, Amy, the cat policy. We really need to talk about the cats, don't we..."

但目前,当我在浏览器中输入 /secrets 时,在根 URL 之后出现了 404 错误。任何人都可以告诉我如何以不同的方式处理这个问题,以便我注册成功的 GET 请求并正确地提供页面吗?

首先在 app.js 中的请求 url 中添加一个 '/': app.use('/secrets', secrets)

现在你仍然定义使用“/secrets/secrets”作为GET,因为 路由器的设置附加了来自 app.js.

的请求 url

将路由器的请求 url 更改为仅 / 就像

router.get('/', function(req, res, next) {} );