一个简单的测试

/**
* 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. TCP网络编程-----客户端请求连接服务器、向服务器发数据、从服务器接收数据、关闭连接

    SOCKET m_sockClient; unsigned short portNum; ------------------------------------------------------- ...

  2. oracle PL/SQL语法基础

    目录 数据类型 定义变量 PL/SQL控制结构 参考资料 Oracle10g数据类型总结 PL/SQL之基础篇 数据类型 学习总结 字符类型 char.nchar.varchar.nvarchar:有 ...

  3. c语言贪吃蛇详解4.食物的投放与蛇的变长

    c语言贪吃蛇详解4.食物的投放与蛇的变长 前几天的实验室培训课后作业我布置了贪吃蛇,今天有时间就来写一下题解.我将分几步来教大家写一个贪吃蛇小游戏.由于大家c语言未学完,这个教程只涉及数组和函数等知识 ...

  4. Linux 笔记 #02# Installing MySQL & Installing the Default JRE/JDK

    Environment: debian 8 Installing MySQL Reference material: https://linode.com/docs/databases/mysql/h ...

  5. ETL实践--Spark做数据清洗

    ETL实践--Spark做数据清洗 上篇博客,说的是用hive代替kettle的表关联.是为了提高效率. 本文要说的spark就不光是为了效率的问题. 1.用spark的原因 (如果是一个sql能搞定 ...

  6. Linux进程管理描述符 task_struct

    转:http://blog.csdn.net/hongchangfirst/article/details/7075026 大家都知道进程,可是知道linux是怎么管理其进程的吗?每一个进程都有一个进 ...

  7. Jfinal拦截器源码解读

    本文对Jfinal拦截器源码做以下分析说明

  8. Codeforces 839A Arya and Bran【暴力】

    A. Arya and Bran time limit per test:1 second memory limit per test:256 megabytes input:standard inp ...

  9. [51nod1197]字符串的数量 V2

    用N个不同的字符(编号1 - N),组成一个字符串,有如下要求: (1) 对于编号为i的字符,如果2 * i > n,则该字符可以作为结尾字符.如果不作为结尾字符而是中间的字符,则该字符后面可以 ...

  10. [bzoj3048] [Usaco2013 Jan]Cow Lineup

    一开始一脸懵逼.. 后来才想到维护一左一右俩指针l和r..表示[l,r]这段内不同种类的数字<=k+1种. 显然最左的.合法的l随着r的增加而不减. 顺便离散化,记一下各个种类数字出现的次数就可 ...