我有一个快速的应用程序设置,但我不知道如何正确路由。目前,我在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请求并正确投放页面?

最佳答案

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

现在,您仍然定义使用“ / secrets / secrets”作为GET,因为
路由器的设置将附加来自app.js的请求网址。

将路由器的请求网址更改为仅/之类的

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

关于javascript - 快速路由-路由到/secrets时出现404错误,我缺少什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38595427/

10-10 22:09