一个简单的测试
var superagent = require('superagent');
var expect = require('expect.js');
describe('express rest api server', function () {
var id;
it('post object', function (done) {
superagent.post('http://localhost:3000/collections/test')
.send({
name: 'James',
email: 'james@qq.com'
})
.end(function (err, res) {
expect(err).to.eql(null);
expect(res.body.length).to.eql(1);
expect(res.body[0]._id.length).to.eql(24);
id = res.body[0]._id;
done();
});
});
it('retrieves an object', function (done) {
superagent.get('http://localhost:3000/collections/test/' + id)
.end(function (err, res) {
expect(err).to.eql(null);
expect( typeof res.body).to.eql('object');
expect(res.body._id).to.eql(id);
done();
});
});
it('retrieves a collection', function (done) {
superagent.get('http://localhost:3000/collections/test')
.end(function (err, res) {
expect(err).to.eql(null);
expect(res.body.length).to.be.above(0);
expect(res.body.map(function (item) {
return item._id;
})).to.contain(id);
done();
});
});
it('updates an object', function (done) {
superagent.put('http://localhost:3000/collections/test/' + id)
.send({
name: 'Bond',
email: 'bond@qq.com'
})
.end(function (err, res) {
expect(err).to.eql(null);
expect(typeof res.body).to.eql('object');
expect(res.body.msg).to.eql('success');
done();
});
});
it('checks an updated object', function (done) {
superagent.get('http://localhost:3000/collections/test/' + id)
.end(function (err, res) {
expect(err).to.eql(null);
expect( typeof res.body).to.eql('object');
expect(res.body._id.length).to.eql(24);
expect(res.body._id).to.eql(id);
done();
});
});
it('remove an object', function (done) {
superagent.del('http://localhost:3000/collections/test/' + id)
.end(function (err, res) {
expect(err).to.eql(null);
expect(typeof res.body).to.eql('object');
expect(res.body.msg).to.eql('success');
done();
});
});
});
一个简单的api
const express = require('express'),
mongoskin = require('mongoskin'),
bodyParser = require('body-parser'),
logger = require('morgan');
const app = express();
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(logger());
const db = mongoskin.db('mongodb://@localhost:27017/test', {safe: true});
const id = mongoskin.helper.toObjectID;
app.param('collectionName', function (req, res, next, collectionName) {
req.collection = db.collection(collectionName);
return next();
});
app.get('/',function (req, res, next) {
res.send('Select a collection,e.g., /collections/messages');
});
app.get('/collections/:collectionName', function (req, res, next) {
req.collection.find({}, {limit: 10, sort: [['_id', -1]]})
.toArray(function (err, results) {
console.info(err);
if (err) return next();
res.send(results);
});
});
app.post('/collections/:collectionName', function (req, res, next) {
req.collection.insert(req.body, {}, function (err, results) {
if (err) return next();
res.send(results);
});
});
app.get('/collections/:collectionName/:id', function (req, res, next) {
req.collection.findOne({_id: id(req.params.id)}, function (err, result) {
console.info(result._id);
if (err) return next();
res.send(result);
});
});
app.put('/collections/:collectionName/:id',function (req, res, next) {
req.collection.update({
_id: id(req.params.id)
}, {
$set: req.body
}, {
safe: true, multi: false
}, function (err, result) {
console.info('Put error: ' + err);
console.info('Put result: ' + result);
if (err) return next();
res.send((result === 1) ? {msg: 'success'} : {msg: 'error'});
});
});
app.del('/collections/:collectionName/:id', function (req, res, next) {
req.collection.remove({_id: id(req.params.id)}, function (err, result) {
if (err) return next();
res.send((result === 1) ? {msg: 'success'} : {msg: 'error'});
});
});
app.listen(3000, function () {
console.log('Server is running');
});
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 手把手教你更优雅的享受 DeepSeek
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现