一个简单的测试

/**
* Created by M.C on 2017/9/8.
*/ 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.length).to.eql(24);
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

/**
* Created by M.C on 2017/9/8.
*/ 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');
});

第一个RESTful API的更多相关文章

  1. 使用python的Flask实现一个RESTful API服务器端

    使用python的Flask实现一个RESTful API服务器端 最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. 本文 ...

  2. 使用python的Flask实现一个RESTful API服务器端[翻译]

    最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. 本文将会使用python的Flask框架轻松实现一个RESTful的服务 ...

  3. 一个Restful Api的访问控制方法

    最近在做的两个项目,都需要使用Restful Api,接口的安全性和访问控制便成为一个问题,看了一下别家的API访问控制办法. 新浪的API访问控制使用的是AccessToken,有两种方式来使用该A ...

  4. 实现一个 RESTful API 服务器

    RESTful 是目前最为流行的一种互联网软件结构.因为它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. 什么是 REST REST(REpresentational Stat ...

  5. 转:使用python的Flask实现一个RESTful API服务器端

    提示:可以学习一下flask框架中对于密码进行校验的部分.封装了太多操作. 最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了 ...

  6. 转:一个Restful Api的访问控制方法(简单版)

    最近在做的两个项目,都需要使用Restful Api,接口的安全性和访问控制便成为一个问题,看了一下别家的API访问控制办法. 新浪的API访问控制使用的是AccessToken,有两种方式来使用该A ...

  7. 使用 SpringBoot 构建一个RESTful API

    目录 背景 创建 SpringBoot 项目/模块 SpringBoot pom.xml api pom.xml 创建 RESTful API 应用 @SpringBootApplication @C ...

  8. 用 Go 快速开发一个 RESTful API 服务

    何时使用单体 RESTful 服务 对于很多初创公司来说,业务的早期我们更应该关注于业务价值的交付,而单体服务具有架构简单,部署简单,开发成本低等优点,可以帮助我们快速实现产品需求.我们在使用单体服务 ...

  9. 使用Flask设计带认证token的RESTful API接口[翻译]

    上一篇文章, 使用python的Flask实现一个RESTful API服务器端  简单地演示了Flask实的现的api服务器,里面提到了因为无状态的原则,没有session cookies,如果访问 ...

随机推荐

  1. BCB F12切换界面 显示异常

      亲们,我偶遇了一个小怪兽F12切换界面,效果如下:       还没有解决办法:

  2. linux 常见操作指令

    1.ssh root@ip ssh 登录 2.ll ls 列出当文件夹下 所以文件 3. cd ./xx 进入 xx 文件夹 4. vim vi xxx 进入 xx文件的 编辑模式. i 开始编辑 e ...

  3. C语言中不同变量的访问方式

    C语言中的变量大致可以分为全局变量,局部变量,堆变量和静态局部变量,这些不同的变量存储在不同的位置,有不同的生命周期.一般程序将内存分为数据段.代码段.栈段.堆段,这几类变量存储在不同的段中,造成了它 ...

  4. flush table with read lock的轻量级解决方案[原创]

    为什么要使用FTWRL   MySQL dba在日常工作中,数据备份绝对是工作频度最高的工作内容之一.当你使用逻辑方式进行备份(mydumper,mysqldump)或物理方式进行备份(percona ...

  5. 【转载】ipcs与Linux共享内存

    一.共享内存相关知识 所谓共享内存,就是多个进程间共同地使用同一段物理内存空间,它是通过将同一段物理内存映射到不同进程的 虚拟空间来实现的.由于映射到不同进程的虚拟空间中,不同进程可以直接使用,不需要 ...

  6. Linux中oops信息调试【转】

    1.Oops 信息来源及格式 Oops 这个单词含义为“惊讶”,当内核出错时(比如访问非法地址)打印出来的信息被称为 Oops 信息. 2.Oops 信息包含以下几部分内容 2.1 一段文本描述信息. ...

  7. web中的简单全选反选

    <html> <body> <table> <tr> <th><input type="checkbox" onc ...

  8. mysqlfront提示过期解决方式

    帮助菜单(help)->登记(registration) 粘贴就好了 gNBpPFgyOw9Rwt/ozsnjgM7tJNo2 bhaaAThangemMkaz2tQhq3/f7dZ7Vj29W ...

  9. SQL数据开发(经典) 基本操作

    数据开发(经典) 1.按姓氏笔画排序: Select * From TableName Order By CustomerName Collate Chinese_PRC_Str oke_ci_as ...

  10. 详解css3弹性盒模型(Flexbox)

    目前没有浏览器支持 box-flex 属性. Firefox 支持替代的 -moz-box-flex 属性. Safari.Opera 以及 Chrome 支持替代的 -webkit-box-flex ...