我正在使用MEAN堆栈作为我的后端。这是我的服务器js文件
const users = require('./routes/users');
app.get('/', (req, res) => {
res.send('Invalid endpoint');
});
这是我的路线文件users.js
router.get('/jobdetail', function(req,res) {
console.log('fetching jobs');
jobDetails.find({}, (err,jobs) => {
if(err) {
console.log(err);
}
else {
res.json(jobs);
}
});
});
当我运行postman bu url:
本地主机:3000 / API /的JobDetail
而不是来自MongoDB的数据我得到了响应
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
<script type="text/javascript" src="http://gc.kis.v2.scr.kaspersky-labs.com/D11634C0-514E-C94E-9DFD-54EBD0B16E5F/main.js" charset="UTF-8"></script>
</head>
<body>
<pre>Cannot GET /api/jobdetail</pre>
</body>
</html>
答案 0 :(得分:0)
var api = require('../routes/users.js');
app.use('/api', api);
您的routes / users.js文件中的:
var mongoose =require('mongoose');
var router = require('express').Router();
var conn = mongoose.connection;
conn.once("open", () => {
router.get('/jobdetail', function(req, res) {
jobDetails.find({}, (err,jobs) => {
if(err) {
console.log(err);
}
else {
res.json(jobs);
}
});
});
});
module.exports = router;