http://www.expressjs.com.cn/php
Express 是一个基于 Node.js 平台的极简、灵活的 web 应用开发框架,它提供一系列强大的特性,帮助你建立各类 Web 和移动设备应用。html
http://www.expressjs.com.cn/starter/hello-world.htmlgit
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });
http://www.ruanyifeng.com/blog/2011/09/restful.htmlgithub
https://www.ibm.com/developerworks/library/ws-restful/index.htmlweb
This article suggests that in its purest form today, when it's attracting this much attention, a concrete implementation of a REST Web service follows four basic design principles:sql
- Use HTTP methods explicitly.
- Be stateless.
- Expose directory structure-like URIs.
- Transfer XML, JavaScript Object Notation (JSON), or both.
http://www.ruanyifeng.com/blog/2014/05/restful_api.htmlmongodb
https://www.cnblogs.com/imyalost/p/7923230.htmltypescript
2、RESTful的特征和优势express
一、客户端-服务器(Client-Server):提供服务的服务器和使用服务的客户端分离解耦;npm
优势:提升客户端的便捷性(操做简单)
简化服务器提升可伸缩性(高性能、低成本)
容许客户端服务端分组优化,彼此不受影响
二、无状态(Stateless):来自客户的每个请求必须包含服务器处理该请求所需的全部信息(请求信息惟一性);
优势:提升可见性(能够单独考虑每一个请求)
提升可靠性(更容易故障恢复)
提升了可扩展性(下降了服务器资源使用)
三、可缓存(Cachable):服务器必须让客户端知道请求是否能够被缓存?若是能够,客户端能够重用以前的请求信息发送请求;
优势:减小交互链接数
减小链接过程的网络时延
四、分层系统(Layered System):容许服务器和客户端之间的中间层(代理,网关等)代替服务器对客户端的请求进行回应,而客户端不须要关心与它交互的组件以外的事情;
优势:提升了系统的可扩展性
简化了系统的复杂性
五、统一接口(Uniform Interface):客户和服务器之间通讯的方法必须是统一化的。(例如:GET,POST,PUT.DELETE)
优势:提升交互的可见性
鼓励单独优化改善组件
六、支持按需代码(Code-On-Demand,可选):服务器能够提供一些代码或者脚本并在客户的运行环境中执行。
优势:提升可扩展性
https://florianholzapfel.github.io/express-restify-mongoose/
From the command line
npm install express-restify-mongoose --save
http://www.cnblogs.com/lkiversonlk/p/4878139.html
Mongo, Express Restful接口搭建
首先安装express-restify-mongoose
npm install express-restify-mongoose --save
而后新建一个router作Restful Service,假设咱们的数据类是Customer,须要一个name字段和一个可选的comment字段.
/* models.js */ var express = require('express'); var router = express.Router(); var mongoose = require("mongoose"); var resify = require("express-restify-mongoose") mongoose.connect("mongodb://localhost/test"); resify.serve(router, mongoose.model('Customer', new mongoose.Schema( { name : {type : String, required : true}, comment : {type : String} } ))) module.exports = router;
而后把这个router注册到express里
/* in app.js */ var models = require("[models.js位置]"); ... app.use(models)
OK,如今运行server测试下: http://localhost:3000/api/v1/Customers,Restful接口已经有了~
GET http://localhost/api/v1/Customer/count GET http://localhost/api/v1/Customer POST http://localhost/api/v1/Customer DELETE http://localhost/api/v1/Customer GET http://localhost/api/v1/Customer/:id GET http://localhost/api/v1/Customer/:id/shallow PUT http://localhost/api/v1/Customer/:id POST http://localhost/api/v1/Customer/:id PATCH http://localhost/api/v1/Customer/:id DELETE http://localhost/api/v1/Customer/:id
其它例子:
https://github.com/florianholzapfel/express-restify-mongoose/blob/master/examples/todos/todos.js
https://github.com/fanqingsong/web_data_visualization
build\webservice_resty.js
例子
const express = require('express') const bodyParser = require('body-parser') const methodOverride = require('method-override') const mongoose = require('mongoose') const restify = require('express-restify-mongoose') const app = express() const router = express.Router() app.use(bodyParser.json()) app.use(methodOverride()) mongoose.connect('mongodb://localhost:27017/zhipin') restify.serve(router, mongoose.model('summary', new mongoose.Schema({ Technology : { type: String }, // 技术名称 Count : { type: Number }, // 技术数目 }))) app.use(router) app.listen(3000, () => { console.log('Express server listening on port 3000') })
测试:
GET http://localhost:3000/api/v1/summary
GET http://localhost:3000/api/v1/summary/5b7ed6b40abe4e3714a7489a