节点js路由不起作用在浏览器上出现错误

时间:2018-11-17 11:31:45

标签: node.js express

服务器文件:

const express = require('express');
const mongoose= require('mongoose');

const app=express();

//DB config
const db= require('./config/key').mongoURI;
mongoose.connect(db)
        .then(()=>console.log("successfully Connected to Mongo DB"))
        .catch(err=>console.log(err));

//routes

app.use('api/users', require('./routes/api/user'));
//app.use('api/profile', require('./routes/api/profile'));
//app.use('api/post', require('./routes/api/post'));

app.get('/',(req,res)=> res.json({ msg: 'general Works' }));
;

const port =process.env.PORT || 5000;

app.listen(port,()=>console.log("Server is runing on "+ port));

路由文件:

const express = require('express');
const router = express.Router();

router.get('/test', (req, res,next) => res.json({ msg: 'user Works' }));

module.exports = router;

当我运行服务器时,它显示错误“我无法尝试获取/ api / users / test的错误”,但总是给出上述错误,但终端未显示任何错误

1 个答案:

答案 0 :(得分:3)

请重构您的代码 来自:app.use('api/users', require('./routes/api/user')); 到:app.use('/api/users', require('./routes/api/user'));