我是NodeJs的新手。我收到以下代码的错误。
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
port = process.env.PORT || 9090,
mongoose = require('mongoose'),
Cheque = require('./models/cheque'),
router = express.Router();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
mongoose.connect('http://localhost:27017/utils/chequeman');
router.route('/cheques').post(function (req, res) {
console.log('u r in cheques.');
var cheque = new Cheque();
cheque.chequeReceiptDate = req.body.chequeReceiptDate;
cheque.save(function (err) {
if (err)
res.send(err)
res.json({ message: 'Cheque details added' });
});
});
app.use('/api', router);
app.listen(port);
console.log('Magin happens at' + port);
当我在邮递员中尝试POST请求时,我得到的响应为"无法POST / api / check"
请帮忙。
答案 0 :(得分:0)
尝试声明您正在使用的变量。
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
let port = process.env.PORT || 9090;
let mongoose = require('mongoose');
let Cheque = require('./models/cheque');
let router = express.Router();
将您的API静态化
app.use('/CHEQUEDIRECTORY', express.static(__dirname + '/CHEQUEDIRECTORY'))
不要忘记在您的API上导出模块。
您是否正确安装了包裹?