我正在尝试使用express.js编写代码来创建mongodb条目。但是,当我使用cURL请求测试我的代码时,我收到错误"来自服务器&#34的空响应。
这是我的express.js代码:
var express = require('express');
var router = express.Router();
var http = require('http');
var url = require('url');
var util = require('util');
var bodyParser = require('body-parser')
var ObjectID = require('mongodb').ObjectID;
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({extended: true}));
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_db');
var channelSchema = mongoose.Schema({
name: String,
verified: String,
private: String,
channelID: String,
handle: String,
subscribers: String
});
var Channel = mongoose.model("Channel", channelSchema);
router.post('/createChannel', bodyParser, function(req, res, next) {
if(!req.body) return res.send(400);
var objID = new ObjectID();
var newChannel = new Channel({
name: req.body["name"],
verified: req.body["verified"],
private: req.body["private"],
channelID: objID,
handle: req.body["handle"],
subscribers: req.body["subscribers"]
});
newChannel.save(function(err, point){
if(err) res.send(500);
else res.send(200);
});
});
module.exports = router;
我不明白为什么请求失败...它总是超时。它与我提供的HTTP响应有关吗?
答案 0 :(得分:0)
它没有失败,只是说“空洞的回应”。这是正确的,因为你发送了一个响应,但它是空的。发回你的观点:res.status(200).send(point)
甚至更好,res.status(200).json(point)
。