Node.js 当GET路径不存在时,如何自动返回404';不存在?
我正在使用NodeJS、Express和handlebar(模板引擎)构建一个web应用程序。目前,每当用户输入一个不存在的URL时(或者当他们可能无法访问该URL时),我都会尝试自动重定向用户 下面返回索引页:Node.js 当GET路径不存在时,如何自动返回404';不存在?,node.js,express,handlebars.js,http-status-code-404,Node.js,Express,Handlebars.js,Http Status Code 404,我正在使用NodeJS、Express和handlebar(模板引擎)构建一个web应用程序。目前,每当用户输入一个不存在的URL时(或者当他们可能无法访问该URL时),我都会尝试自动重定向用户 下面返回索引页: router.get('/', (req, res) => { res.render('index/index'); }); 但是我如何制作这样的东西: router.get('/:ThisCouldBeAnything', (req, res) => {
router.get('/', (req, res) => {
res.render('index/index');
});
但是我如何制作这样的东西:
router.get('/:ThisCouldBeAnything', (req, res) => {
res.render('errors/404');
});
以下示例来自Github:
假设我输入此URL:
它会自动返回404。如何在我的应用程序中实现这一点
提前感谢。在完成所有其他路线后,您可以添加:
app.get('*', (req, res) => {
res.render('errors/404');
});
或者,您可以在所有其他中间件和路由之后使用中间件功能
app.use((req, res) => {
res.render('errors/404');
});
因此,您可能会得到如下结果:
//body-parser, cookie-parser, and other middleware etc up here
//routes
app.get('/route1', (req, res) => {
res.render('route1');
});
app.get('/route2', (req, res) => {
res.render('route2');
});
//404 handling as absolute last thing
//You can use middleware
app.use((req, res) => {
res.render('errors/404');
});
//Or a catch-all route
app.get('*', (req, res) => {
res.render('errors/404');
});
在所有其他路线之后,您可以添加:
app.get('*', (req, res) => {
res.render('errors/404');
});
或者,您可以在所有其他中间件和路由之后使用中间件功能
app.use((req, res) => {
res.render('errors/404');
});
因此,您可能会得到如下结果:
//body-parser, cookie-parser, and other middleware etc up here
//routes
app.get('/route1', (req, res) => {
res.render('route1');
});
app.get('/route2', (req, res) => {
res.render('route2');
});
//404 handling as absolute last thing
//You can use middleware
app.use((req, res) => {
res.render('errors/404');
});
//Or a catch-all route
app.get('*', (req, res) => {
res.render('errors/404');
});
我看你有特快专车。您所要做的就是包含一个默认处理程序,其中包括
res.status(404).render('404template')
比如说
app.get('*', (req, res,next) => {
res.status(404).render('error.ejs')
});
我看你有特快专车。您所要做的就是包含一个默认处理程序,其中包括
res.status(404).render('404template')
比如说
app.get('*', (req, res,next) => {
res.status(404).render('error.ejs')
});
在所有路由处理程序之后使用中间件捕获不存在的路由:
app.get('/some/route', function (req, res) {
...
});
app.post('/some/other/route', function (req, res) {
...
});
...
// middleware to catch non-existing routes
app.use( function(req, res, next) {
// you can do what ever you want here
// for example rendering a page with '404 Not Found'
res.status(404)
res.render('error', { error: 'Not Found'});
});
在所有路由处理程序之后使用中间件捕获不存在的路由:
app.get('/some/route', function (req, res) {
...
});
app.post('/some/other/route', function (req, res) {
...
});
...
// middleware to catch non-existing routes
app.use( function(req, res, next) {
// you can do what ever you want here
// for example rendering a page with '404 Not Found'
res.status(404)
res.render('error', { error: 'Not Found'});
});
对不起,尤内尔。我不是想偷你的答案。我一定是在你写你的文章时编辑了我的文章。而且,看起来我在你写答案前几分钟发布了我的编辑建议。但我更充实的例子是在你的答案之后发布的。不,你的第一篇文章是关于使用星号的,它不起作用,因为它匹配所有路线是的,没错。但我添加中间件建议的地方是在你回答之前。对不起,YouneL。我不是想偷你的答案。我一定是在你写你的文章时编辑了我的文章。而且,看起来我在你写答案前几分钟发布了我的编辑建议。但我更充实的例子是在你的答案之后发布的。不,你的第一篇文章是关于使用星号的,它不起作用,因为它匹配所有路线是的,没错。我的编辑,我添加了中间件的建议,但是,在你的答案之前。谢谢你的快速回复。我将使用你的中间件功能!我相信我还不能这样做,因为我是新来的。我做了,但它说:记录了那些声誉低于15的人所投的票,但不改变公开显示的帖子分数。另一种方法是使用
http errors
,如感谢您的快速回复所述。我将使用你的中间件功能!我相信我还不能这样做,因为我是新来的。我做了,但它说:记录了那些声誉低于15的人所投的票,但不改变公开显示的帖子分数。另一种方法是使用http errors
,如上面所述,这不适用于正斜杠“/”,它不应该是“*”就像上面的回答?这不适用于正斜杠“/”,它不应该像上面的回答一样是“*”吗?