Sapper:将中间件的 "ignore" 目录指向“_error.svelte”
Sapper: point middleware's "ignore" dirs to "_error.svelte"
我已将 sapper 设置为按路由忽略某些目录,如下所示:
polka()
.use(
sapper.middleware({
// Exclude components from routing
ignore: ['/admin', '/components'],
)
.listen(PORT);
现在,在 http://localhost:3000/admin 和 http://localhost:3000/components 中浏览浏览器,它会打印一个:
Not Found
我可以将这些路径指向常规 /routes/_error.svelte
并以某种方式出现 404 错误吗?
问题是,ignore
确实导致 Sapper 忽略请求。当它看到这些路由之一时,它在内部只调用 next()
。您可以通过将路由更改为不存在的路由来获得所需的行为:
function handle_ignored(req, res, next) {
if (['/admin', '/components'].some(path => req.path.startsWith(path))) {
req.path = '/_error'; // ... or any other non-existent path
}
next();
}
polka()
.use(
handle_ignored,
sapper.middleware()
)
.listen(PORT, err => {
if (err) console.log('error', err);
});
我已将 sapper 设置为按路由忽略某些目录,如下所示:
polka()
.use(
sapper.middleware({
// Exclude components from routing
ignore: ['/admin', '/components'],
)
.listen(PORT);
现在,在 http://localhost:3000/admin 和 http://localhost:3000/components 中浏览浏览器,它会打印一个:
Not Found
我可以将这些路径指向常规 /routes/_error.svelte
并以某种方式出现 404 错误吗?
问题是,ignore
确实导致 Sapper 忽略请求。当它看到这些路由之一时,它在内部只调用 next()
。您可以通过将路由更改为不存在的路由来获得所需的行为:
function handle_ignored(req, res, next) {
if (['/admin', '/components'].some(path => req.path.startsWith(path))) {
req.path = '/_error'; // ... or any other non-existent path
}
next();
}
polka()
.use(
handle_ignored,
sapper.middleware()
)
.listen(PORT, err => {
if (err) console.log('error', err);
});