路由,如何在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未找到?”
发布评论