express + restful
express
http://www.expressjs.com.cn/
Express 是一个基于 Node.js 平台的极简、灵活的 web 应用开发框架,它提供一系列强大的特性,帮助你创建各种 Web 和移动设备应用。
http://www.expressjs.com.cn/starter/hello-world.html
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);
});
restful api
http://www.ruanyifeng.com/blog/2011/09/restful.html
https://www.ibm.com/developerworks/library/ws-restful/index.html
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:
- 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.html
https://www.cnblogs.com/imyalost/p/7923230.html
二、RESTful的特征和优点
1、客户端-服务器(Client-Server):提供服务的服务器和使用服务的客户端分离解耦;
优点:提高客户端的便捷性(操作简单)
简化服务器提高可伸缩性(高性能、低成本)
允许客户端服务端分组优化,彼此不受影响
2、无状态(Stateless):来自客户的每一个请求必须包含服务器处理该请求所需的所有信息(请求信息唯一性);
优点:提高可见性(可以单独考虑每个请求)
提高可靠性(更容易故障恢复)
提高了可扩展性(降低了服务器资源使用)
3、可缓存(Cachable):服务器必须让客户端知道请求是否可以被缓存?如果可以,客户端可以重用之前的请求信息发送请求;
优点:减少交互连接数
减少连接过程的网络时延
4、分层系统(Layered System):允许服务器和客户端之间的中间层(代理,网关等)代替服务器对客户端的请求进行回应,而客户端不需要关心与它交互的组件之外的事情;
优点:提高了系统的可扩展性
简化了系统的复杂性
5、统一接口(Uniform Interface):客户和服务器之间通信的方法必须是统一化的。(例如:GET,POST,PUT.DELETE)
优点:提高交互的可见性
鼓励单独优化改善组件
6、支持按需代码(Code-On-Demand,可选):服务器可以提供一些代码或者脚本并在客户的运行环境中执行。
优点:提高可扩展性
express + restful
https://florianholzapfel.github.io/express-restify-mongoose/
Easily create a flexible REST interface for mongoose models
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

express + restful的更多相关文章
- 基于TypeScript装饰器定义Express RESTful 服务
前言 本文主要讲解如何使用TypeScript装饰器定义Express路由.文中出现的代码经过简化不能直接运行,完整代码的请戳:https://github.com/WinfredWang/expre ...
- postman测试express restful接口
安装express及postman var express = require('express') var app = express(); var calculation = require('. ...
- Node.js实现RESTful api,express or koa?
文章导读: 一.what's RESTful API 二.Express RESTful API 三.KOA RESTful API 四.express还是koa? 五.参考资料 一.what's R ...
- Mongo, Express, Angular, Node-- MEAN Stack搭建
前言 作为一个从后端转全栈的码农,我一直使用express,jade & bootstrap, jquery的组合.重复了几次相同的工作后,看到网上开始流行MEAN Stack,于是也对其研究 ...
- NodeJs - 100
Nodejs官方文档 https://nodejs.org/en/docs/ Nodejs官方网站 https://nodejs.org/en/ Nodejs的特征: 1.采用非阻塞性IO机制:—— ...
- 使用 Express 和 waterline 创建简单 Restful API
这几篇都是我原来首发在 segmentfault 上的地址:https://segmentfault.com/a/1190000004996659 突然想起来我这个博客冷落了好多年了,也该更新一下, ...
- [转] Creating a Simple RESTful Web App with Node.js, Express, and MongoDB
You can find/fork the sample project on GitHub Hey! This and all my other tutorials will soon be mov ...
- nodejs+express+mysql实现restful风格的增删改查示例
首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...
- 使用Express构建RESTful API
RESTful服务 REST(Representational State Transfer)的意思是表征状态转移,它是一种基于HTTP协议的网络应用接口风格,充分利用HTTP的方法实现统一风格接口的 ...
随机推荐
- 2017 百度杯丶春秋欢乐赛 writeup
1. 内涵图(Misc) 题目: 我不是一个简单的图片 我是一个有内涵的图片 解:保存到桌面,右键属性->详细信息,即可获得flag. 2. 小电影(Misc) 题目: 我说过 这次比赛是让大家 ...
- python基础篇实战
1. 判断下面的结果 # 1. 判断下面的结果 # 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 pri ...
- Java之匿名内部类详解
前言 本文讲解Java中最后一种内部类,叫做匿名内部类.顾名思义,所谓的匿名内部类就是一个没有显式的名字的内部类,在实际开发中,此种内部类用的是非常多的. 匿名内部类 本质:匿名内部类会隐式的继承一个 ...
- Cordova入门系列(三)Cordova插件调用
版权声明:本文为博主原创文章,转载请注明出处 上一章我们介绍了cordova android项目是如何运行的,这一章我们介绍cordova的核心内容,插件的调用.演示一个例子,通过cordova插件, ...
- 前端——HTML
HTML HTML叫做超文本标记语言,是一种制作万维网页面标准语言.相当于定义一套规则,大家都来遵守它,这样浏览器就可以去解释它. 浏览器负责将标签翻译成用户看得懂的格式,呈现给用户. 作为开发者需要 ...
- Linux内核入门到放弃-Ext2数据结构-《深入Linux内核架构》笔记
Ext2文件系统 物理结构 结构概观 块组是该文件系统的基本成分,容纳了文件系统的其他结构.每个文件系统都由大量块组组成,在硬盘上相继排布: ----------------------------- ...
- 日版iphone5 SB 配合REBELiOS卡贴破解电信3G步骤
1.插入贴膜卡和sim卡:进入“设置—电话—sim卡应用程序”选择CDMA电信解锁: 2.越狱设备,添加cydia.gpplte.com源,安装“6S/6/5S/5C/5电信新补丁”: 3.打卡gpp ...
- SkylineGlobe7.0.1版本 支持SQLite(*.sqlite;*.db)数据库
SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 ...
- UOJ188 Sanrd Min_25筛
传送门 省选之前做数论题会不会有Debuff啊 这道题显然是要求\(1\)到\(x\)中所有数第二大质因子的大小之和,如果不存在第二大质因子就是\(0\) 线性筛似乎可以做,但是\(10^{11}\) ...
- TypeError: sequence item 1: expected str instance, int found
Error Msg Traceback (most recent call last): File "E:/code/adva_code/my_orm.py", line 108, ...