我在localhost上运行了一个非常好的api,我在Mocha中测试过(启动服务器) - 所有测试似乎都通过了。但是,如果我尝试在restful url上使用POSTMAN或curl,则会失败(请参阅本页底部的错误)。我已经尝试过Firefox和Chrome。
EXPRESS
var mongoose = require('mongoose');
var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.post('/uborrow/items', handlers.items.addItem);
blah blah blah startserver on port 3000
SCHEMA
var itemSchema = new Schema({
name: { type: String, required: true, index : {unique:true}},
description: String,
thumbnail : Buffer,
waitList : [],
checkoutHistory : [],
currentCheckout : String,
lastCheckoutDate : Date,
availableDate: { type: Date, default: Date.now },
inactiveDate : Date,
cabinet : ObjectId
});
itemSchema.statics.createItem = function(item, cb) {
console.log("Adding item " + item);
var itemModel = mongoose.model("Item");
new itemModel(item).save( function (err, newItem){
if (err) cb(err, null);
cb(null,newItem);
})
};
HANDLER
console.log("\nHEADERS - item:" + req.body.item )
Item.createItem(req.body.item, function(err,item){
if (err) return res.json(401,err);
res.json(202,item);
})
});
TESTDATA
module.exports = {
"itemData" : {
availableTrialItem3 : {
name:"SE 8399-RH-ROCK 11-Inch Rock Pick Hammer",
description: "Brand new 20 oz. Rock Pick Hammer. This heavy, well balanced hammer has a rubberized hammer for a comfortable grip. This hammer is made of a single-Piece of drop forged metal.",
availableDate : new Date(new Date().setDate(new Date().getDate()-10)),
waitList : []
}
}
}
TEST
var itd = testData.itemData;
it.only("should return 202 item created item via post to /uborrow/items", function(done){
var url = "/uborrow/items"
request(app)
.post(url)
.send({item:itd.availableTrialItem3})
.end(function (err, res) {
should.not.exist(err);
res.should.have.status(202) ;
res.body.should.property("name").eql(itd.availableTrialItem3.name);
res.body.should.have.property("waitList").with.length(0);
done();
});
});
测试结果
Express server listening on port 3000
UBorrow API :
-- itema api --
connected to test database
ADDING ITEM
Adding item [object Object]
POST /uborrow/items 202 17ms - 391b
â should return 202 item created item via post to /uborrow/items
1 passing (58ms)
邮差
POST /uborrow/items HTTP/1.1
Host: localhost:3000
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="item"
{name:"NORLAKE 119958 Solar Thermometer, 8' Lead Sensor", description: "119958, THERMOMETER SOLAR WITH 8 FOOT SENSOR LEAD. Norlake Genuine OEM replacement part. Norlake has become a leader in high-quality refrigeration for the commercial foodservice operating. Use genuine OEM parts for safety reliability and performance."}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
RESULT
TypeError: Cannot use 'in' operator to search for '_id' in {name:"NORLAKE 119958 Solar Thermometer, 8' Lead Sensor", description: "119958, THERMOMETER SOLAR WITH 8 FOOT SENSOR LEAD. Norlake Genuine OEM replacement part. Norlake has become a leader in high-quality refrigeration for the commercial foodservice operating. Use genuine OEM parts for safety reliability and performance."}