admin管理员组文章数量:1026989
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.
I have a routes/index.js file:
module.exports = function(app){
app.use('/', require('./routes/home'));
app.use('/about', require('./routes/about'));
}
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;
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;
When I go to '/about' I see this error in the browser - 'Cannot GET /about'
Both the home.html and about.html files are located in the same views directory.
Any help here would be very appreciated!
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.
I have a routes/index.js file:
module.exports = function(app){
app.use('/', require('./routes/home'));
app.use('/about', require('./routes/about'));
}
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;
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;
When I go to '/about' I see this error in the browser - 'Cannot GET /about'
Both the home.html and about.html files are located in the same views directory.
Any help here would be very appreciated!
Share Improve this question edited Aug 11, 2016 at 22:22 Jared asked Aug 11, 2016 at 22:17 JaredJared 6612 gold badges7 silver badges19 bronze badges 2 |2 Answers
Reset to default 34let me quote from express doc:
A route will match any path that follows its path immediately with a “/”. For example: app.use('/apple', ...) will match “/apple”, “/apple/images”, “/apple/images/news”, and so on. see express doc
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)..
now just change the /about
in the routes/about.js
then rerun and try to request /about
and it will work :)
Your route is set to /about/about
.
Change about.js
to this:
const express = require('express');
const router = express.Router();
router.get('/', function(req, res) {
res.render('app/about');
});
module.exports = router;
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.
I have a routes/index.js file:
module.exports = function(app){
app.use('/', require('./routes/home'));
app.use('/about', require('./routes/about'));
}
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;
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;
When I go to '/about' I see this error in the browser - 'Cannot GET /about'
Both the home.html and about.html files are located in the same views directory.
Any help here would be very appreciated!
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.
I have a routes/index.js file:
module.exports = function(app){
app.use('/', require('./routes/home'));
app.use('/about', require('./routes/about'));
}
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;
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;
When I go to '/about' I see this error in the browser - 'Cannot GET /about'
Both the home.html and about.html files are located in the same views directory.
Any help here would be very appreciated!
Share Improve this question edited Aug 11, 2016 at 22:22 Jared asked Aug 11, 2016 at 22:17 JaredJared 6612 gold badges7 silver badges19 bronze badges 2-
"Not working" isn't a very good problem description. What happens when you trying to access
http://your.server/about
? Do you see any errors? – Mike Cluck Commented Aug 11, 2016 at 22:19 - Yes, sorry for the vague description. Getting an error on the page - "Cannot GET /about". No errors in the browser console or the terminal – Jared Commented Aug 11, 2016 at 22:21
2 Answers
Reset to default 34let me quote from express doc:
A route will match any path that follows its path immediately with a “/”. For example: app.use('/apple', ...) will match “/apple”, “/apple/images”, “/apple/images/news”, and so on. see express doc
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)..
now just change the /about
in the routes/about.js
then rerun and try to request /about
and it will work :)
Your route is set to /about/about
.
Change about.js
to this:
const express = require('express');
const router = express.Router();
router.get('/', function(req, res) {
res.render('app/about');
});
module.exports = router;
本文标签: javascriptNodeExpressCANNOT GET routeStack Overflow
版权声明:本文标题:javascript - Node, Express - CANNOT GET route - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1737588558a1481853.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
http://your.server/about
? Do you see any errors? – Mike Cluck Commented Aug 11, 2016 at 22:19