第一个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,如果访问 ...
随机推荐
- 第十七章:Python の Web开发基础(四) MVC与Django
本課主題 MVC 介绍 Django 介紹 MVC 介绍 controllers 处理用户请求 views 放置HTML模版 models 操作数据库 MVC框架就是目录的归类 MVC 是一种软件开发 ...
- windows 配置接收报文是否中断
作用:网络编程的时候,编程接收报文,可以不用循环等待并判断是否报文接收完整.配置了windows禁用网络中端后,自己写的程序一次接收,便是整条报文. 步骤: 1."打开网络和共享中心&quo ...
- Oracle 存储过程以及存储函数
以下的一些例子是基于scott用户下的emp表的数据,一和二使用的均为in,out参数,最后一个综合练习使用了 in out参数 一.存储过程 1.创建无参的存储过程示例 ------ hello ...
- linux搭建SS服务
基本准备: 购买主机:www.virmach.com LINUX系统操作经验:vim , apt-get 等命令的使用 putty.exe连接ssh工具的使用 开始 使用putty连接上去,并输入密码 ...
- MySQL两阶段提交
参数介绍 innodb_flush_log_at_trx_commit 0: 每隔1s,系统后台线程刷log buffer,也就是把redo日志刷盘,这里会调用fsync,所以可能丢失最后1s的事务. ...
- 从Unity中的Attribute到AOP(七)
本章我们将依然讲解Unity中的Attribute,继续命名空间在UnityEngine里的. PropertyAttribute,这个特性主要来控制变量或者类在Inspector里面的显示方式.和P ...
- flask动态url规则
动态URL规则 URL规则可以添加变量部分,也就是件更符合同规则的URL抽象成一个URL模式. @app.route('/item/<id>') def item(id): return ...
- mybatis注解开发,动态sql
在利用mybatis注解开始时,如果没有用到动态sql时,可以直接写 @Select("select * from order") List<XlSubOrder> g ...
- web中的简单全选反选
<html> <body> <table> <tr> <th><input type="checkbox" onc ...
- 什么是CDN加速?(转载)
随着互联网的发展,用户在使用网络时对网站的浏览速度和效果愈加重视,但由于网民数量激增,网络访问路径过长,从 而使用户的访问质量受到严重影响.特别是当用户与网站之间的链路被突发的大流量数据拥塞时,对于异 ...