快速节点-无法获取路线 [英] Node, Express - CANNOT GET route

查看:96
本文介绍了快速节点-无法获取路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Express应用程序,并且在路由方面遇到一些问题。我的‘/’路线运行正常,但是其他路线则无效。我调查了其他人发布的问题,但这些问题仍未解决。

I am building an Express app and having some issues with routing. My '/' route is working perfectly, however other routes are not. I've looked into other questions people have posted and these have not resolved my issues.

我有一个route / index.js文件:

I have a routes/index.js file:

module.exports = function(app){
  app.use('/', require('./routes/home'));
  app.use('/about', require('./routes/about'));
}

我的路线/home.js:-工作中!

My routes/home.js: - WORKING!

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

router.get('/', function(req, res) {
   res.render('app/home');
});

module.exports = router;

我的路线/about.js:-不起作用!

My routes/about.js: - NOT WORKING!

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

router.get('/about', function(req, res) {
   res.render('app/about');
});

module.exports = router;

当我转到 / about时,我在浏览器中看到此错误-无法获取/ about '

When I go to '/about' I see this error in the browser - 'Cannot GET /about'

home.html和about.html文件都位于相同的views目录中。

Both the home.html and about.html files are located in the same views directory.

这里的任何帮助将不胜感激!

Any help here would be very appreciated!

推荐答案

让我引用快递doc中的报价:

let me quote from express doc:


一条路线将使用 /匹配紧随其后的任何路径。例如:app.use(’/ apple',...)将匹配 / apple, / apple / images, / apple / images / news,依此类推。
参见快递文档

这是无效的,因为您在 app.use <中设置了 / about 。 / code>和 router.get 中。尝试请求 / about / about ,您会看到它正在运行(只是不如您所愿)。

this is "not working" because you set the /about in the app.use and in the router.get. try to request /about/about and you will see that this is working (just not as you wanted to)..

现在只需在 routes / about.js 中更改 / about ,然后重新运行并尝试请求 / about ,它将起作用:)

now just change the /about in the routes/about.js then rerun and try to request /about and it will work :)

这篇关于快速节点-无法获取路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆