简体   繁体   English

404页使用javascript node.js

[英]404 pages using javascript node.js

I am learning how to redirect a user to a 404 page when an ending doesn't lead to any page.我正在学习如何在结尾没有指向任何页面时将用户重定向到 404 页面。 I am stuck on how to redirect the user to the page.我被困在如何将用户重定向到页面上。 I am not sure if I got the URL ending part input wrong or if I got the send file part wrong.我不确定 URL 结尾部分输入是否错误,或者发送文件部分是否错误。

This is what I have tried so far:这是我到目前为止所尝试的:

    app.get('/:type', (req, res)=> {
    let path = require('path');
    res.status(404).sendFile('404.html');
    });

If you need to handle 404, you might need to add this route at the end of app.js:如果您需要处理 404,您可能需要在 app.js 的末尾添加此路由:

//The 404 Route (ALWAYS Keep this as the last route)
app.get('*', (req, res) => {
  res.status(404).send('Not Found');
});

Or,或者,

app.use((req, res, next) => {
  const err = new httpError(404)
  return next(err);
});

If you are using express , it will be handled by it.如果您使用的是express ,它将由它处理。 You can check this link and it has many options almost similar to each other if the above doesn't work.您可以检查链接,如果上述方法不起作用,它有许多几乎相似的选项。

Assuming you're using express.js.假设您使用的是 express.js。 The Express FAQ answers this question. Express FAQ回答了这个问题。 Look under "How do I handle 404 responses?"查看“如何处理 404 响应?”

In your case, something like this should work:在你的情况下,这样的事情应该有效:

app.use(function (req, res, next) {
  res.status(404).sendFile('./pathTo/404.html');
});

Seems like you just forgot to use the path.join function after importing it;)好像您只是忘记使用 path.join function 后导入它;)

Don't forget to join the dirname( __dirname) with your filename.不要忘记将 dirname( __dirname) 与您的文件名连接起来。

const path = require('path');
app.get('*', (req, res)=> {
    res.status(404).sendFile(path.join(__dirname, "/404.html"));
});

use it after all your routes not before them.在所有路线之后使用它而不是在它们之前。

if you want to send this file just in case of a get request then this should do it but if you want it for all requests then you should use app.use in place of app.get如果您想发送此文件以防出现 get 请求,那么应该这样做,但如果您希望它用于所有请求,那么您应该使用 app.use 代替 app.get

I think you are confused between front-end routing and back-end routing, please refer this .我认为您对前端路由和后端路由感到困惑,请参考this They are handled separately, and you are kind of mixing them together.它们是分开处理的,你有点把它们混合在一起。

From what I understood, that you are trying to redirect the user to a 404 custom html error page you can do that like this but in front-end side (ANGULAR).据我了解,您正在尝试将用户重定向到 404 自定义 html 错误页面,您可以这样做,但在前端(角度)。

{path:"**",pathMatch:"full",redirectTo:"/home"}

Also its better to redirect them to "home" page instead of error page for better user experience.此外,最好将它们重定向到“主页”页面而不是错误页面,以获得更好的用户体验。 404 custom-error page could be used for different purposes such as network failure, server down,code break-down(Note: they are also for better user experience.) 404自定义错误页面可用于不同目的,例如网络故障,服务器停机,代码故障(注意:它们也是为了更好的用户体验。)

In node.js its an endpoint and these APIs are called internally by developers, you get a request either you have the response or you don't, in case you don't you can do something like this在 node.js 中,它是一个端点,这些 API 由开发人员在内部调用,您会收到一个请求,要么您有响应,要么没有响应,如果您没有,您可以做这样的事情

 app.get('*', (req, res) => { res.status(404).send('Not Found'); });

If you found this answer useful please accept it如果你觉得这个答案有用请采纳

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM