最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin24浏览0评论

如果输入错误的路由,如何在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;
发布评论

评论列表(0)

  1. 暂无评论