第一个RESTful API
一个简单的测试
/**
* 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的更多相关文章
- 使用python的Flask实现一个RESTful API服务器端
使用python的Flask实现一个RESTful API服务器端 最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. 本文 ...
- 使用python的Flask实现一个RESTful API服务器端[翻译]
最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. 本文将会使用python的Flask框架轻松实现一个RESTful的服务 ...
- 一个Restful Api的访问控制方法
最近在做的两个项目,都需要使用Restful Api,接口的安全性和访问控制便成为一个问题,看了一下别家的API访问控制办法. 新浪的API访问控制使用的是AccessToken,有两种方式来使用该A ...
- 实现一个 RESTful API 服务器
RESTful 是目前最为流行的一种互联网软件结构.因为它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. 什么是 REST REST(REpresentational Stat ...
- 转:使用python的Flask实现一个RESTful API服务器端
提示:可以学习一下flask框架中对于密码进行校验的部分.封装了太多操作. 最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了 ...
- 转:一个Restful Api的访问控制方法(简单版)
最近在做的两个项目,都需要使用Restful Api,接口的安全性和访问控制便成为一个问题,看了一下别家的API访问控制办法. 新浪的API访问控制使用的是AccessToken,有两种方式来使用该A ...
- 使用 SpringBoot 构建一个RESTful API
目录 背景 创建 SpringBoot 项目/模块 SpringBoot pom.xml api pom.xml 创建 RESTful API 应用 @SpringBootApplication @C ...
- 用 Go 快速开发一个 RESTful API 服务
何时使用单体 RESTful 服务 对于很多初创公司来说,业务的早期我们更应该关注于业务价值的交付,而单体服务具有架构简单,部署简单,开发成本低等优点,可以帮助我们快速实现产品需求.我们在使用单体服务 ...
- 使用Flask设计带认证token的RESTful API接口[翻译]
上一篇文章, 使用python的Flask实现一个RESTful API服务器端 简单地演示了Flask实的现的api服务器,里面提到了因为无状态的原则,没有session cookies,如果访问 ...
随机推荐
- JAVA定时任务调度之Timer入门详解(一)
所谓的Timer,打开jdk的api文档可以看到它的定义:一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行.通俗点讲就是说:有且仅有一个后台线程对多个业务线程进行 ...
- 自学Aruba1.3-WLAN一些基本常识802.11n速率计算方式、802.11n及802.11AC速率表
点击返回:自学Aruba之路 自学Aruba1.3-WLAN一些基本常识802.11n速率计算 1. 802.11n速率计算方式1.1 802.11n使用的主要技术 802.11n采用MIMO多天线技 ...
- Java学习笔记11(面向对象四:多态)
多态: 举例:描述一个事物的多种形态,如Student类继承了Person类,一个Student对象既是Student,又是Person 多态体现为:父类引用变量可以指向子类对象 多态的前提:必须有子 ...
- CSS(二) 颜色 盒模型 文字相关 border
一.颜色 rgb(r,g,b) rgb取值 0-255 分别是 色光三元色 red green blue rgba(r,g,b,a) rgb同上 a 是 alpha 代表透明度 colot ...
- SQL Server之LEFT JOIN、RIGHT LOIN、INNER JOIN的区别
很多人刚入门的时候分不清LEFT JOIN.RIGHT LOIN 和 INNER JOIN的区别,对它们的定义比较模糊,今天就简单的介绍一下它们的区别,对于入门的人来说,应该能够帮助你们理解. lef ...
- three.js 相机
图形学中的相机定义了三维空间到二维屏幕的投影方式,根据投影方式的不同,相机可分为 正交投影相机 与 透视投影相机. 正交投影相机 : 近处.远处的物体大小尺寸保持一致,常适用于工程制图.建模软件,如C ...
- 积累jquery一些有意思的函数
$("#btn").unbind("click"); // 让btn这个元素的点击事件失效 $("#btn").unbind(); // 让 ...
- 前端请求参数MD5加密校验,参数串解密
首先引入MD5加密库:=>https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js; 步骤:=>1.请求前对参数进行字典升序排序,排 ...
- bzoj:1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会
Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...
- c语言基础学习02_windows系统下的cmd命令
=============================================================================注意:cmd的命令很多,需要用的时候可以查询即 ...