【问题标题】:Express routing -- 404 error when routing to /secrets, what am I missing快速路由 - 路由到 /secrets 时出现 404 错误,我错过了什么
【发布时间】:2016-07-26 16:37:12
【问题描述】:

我有一个快速应用设置,但我不知道如何使其正确路由。目前我在 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..."

但目前,当我在浏览器的根 URL 之后键入 /secrets 时,我收到了 404 错误。谁能告诉我如何以不同的方式处理此问题,以便我注册成功的 GET 请求并正确提供页面?

【问题讨论】:

  • 它可能是因为/routes/secrets.js中的语法错误而崩溃,它应该是res.render('secrets', {title: 'Colour Hack v1.1});
  • @DrakaSAN 谢谢。我会改变它,看看它是否有帮助:)......不;我在文件中有这个权利。只是我的一个转录错误,抱歉 DranSAN :/.

标签: javascript node.js templates express http-status-code-404


【解决方案1】:

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

现在您仍然定义使用“/secrets/secrets”作为 GET,因为 路由器的设置从 app.js 附加这个请求 url。

将Router的请求url改为/like

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

【讨论】:

  • 谢谢哈迪。我试过了,但它现在仍然无法正常工作。有没有办法可以跟踪浏览器中实际请求的内容?我正在使用 Iceweasel,我认为它基本上是 Firefox。
  • 没关系。我重新启动了服务器,现在它工作正常。谢谢!
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 2014-10-26
  • 1970-01-01
  • 2017-07-07
相关资源
最近更新 更多