如果输入错误的路由,如何在Node.js中显示“ 404未找到?”

编程入门 行业动态 更新时间:2024-10-05 13:24:07

如果输入错误的<a href=https://www.elefans.com/category/jswz/34/1771390.html style=路由,如何在Node.js中显示“ 404未找到?”"/>

如果输入错误的路由,如何在Node.js中显示“ 404未找到?”

我在这里有一个小问题。如果输入了错误的路线,我想显示404 not found

下面的代码仅在我转到404 not found /时显示[C0

但是当我输入http://localhost:3000时显示http://localhost:3000/wrongroute而不是Cannot GET /wrongroute

404 not found
回答如下:

在END添加此路线。

const bodyParser = require("body-parser");
const mysql = require('mysql');
const router = express.Router();

const db = mysql.createConnection({
    host: 'xxx.xx.xx.xx',
    user: 'root',
    password: '12345',
    database: 'test'
});

db.connect((err) =>{
    if(err){
        throw err;
    }
    console.log('Mysql Connected');
    // res.send("Mysql Connected");
});

router.post('/login', function (req, res) {
      res.send("This is from login route");
   res.end();
})

router.post('/signup', function (req, res) {
   res.send("This is from signup route");
   res.end();
})

router.get('/', function(req, res){
 res.send("404 not found");
});


module.exports = router;

更多推荐

如果输入错误的路由,如何在Node.js中显示“ 404未找到?”

本文发布于:2024-05-07 04:58:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   未找到   错误   如何在   Node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!