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

原文地址:https://www.cnblogs.com/lightsong/p/9563360.html

时间: 2024-08-01 19:33:32

express + restful的相关文章

Node.js实现RESTful api,express or koa?

文章导读: 一.what's RESTful API 二.Express RESTful API 三.KOA RESTful API 四.express还是koa? 五.参考资料 一.what's RESTful API 1.1 RESTful架构 要理解什么是RESTful API我们可以先看一下什么是RESTful架构. REST是Representational State Transfer的缩写,我们可以理解为它的含义是“表现层状态转化”,wikipedia是这样说的:“an archi

[Express + Webstrom] Debug Node.js RESTful application

Using WebStrom can easily debug the Node applcation. For example, we have an Node+Express application. server.js: /** * Created by Answer1215 on 12/9/2014. */ 'use strict'; var expres = require('express'); var app = expres(); app.get('/', function(re

进入全屏 nodejs+express+mysql实现restful风格的增删改查示例

首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间接触到node.js,于是花了两天时间学了一下node.js并写了一个CRUD简单示例.由于前几天一直学用github pages搭建博客,一直没时间写README,今天有空补了上来. 下面来内容自于项目的README. 二.项目介绍 基于node.js + express + mysql实现的re

利用express搭建一个restful api 服务器

学习express有几天了,记录一下内容也给入门的兄弟们一点提示. 想要解决的问题: node开发过程中自动重启服务器,无需每次都要运行node index.js启动服务器(nodemon) 使用ES6,ES7 编写代码,自动编译 使用import,export新特性.(babel) 使用express开发RESTFUL API,模块化开发. 学习postman中的提交内容方式. 利用mongoose中间件来实现model层映射. 利用body-parser中间件来实现提交内容解析.(multi

使用React JS和PHP RESTful进行PayPal Express Checkout

目前市场上有几种付款方式可供选择.PayPal结账选项就是其中之一.在我之前的文章中,我已经讨论了几个支付选项 - 使用PHP的Braintree PayPal,支付系统和使用PHP和MySQL的PayPal Express Checkout.这些是迄今为止观看次数最多的热门文章.今天,让我们看看PayPal Express Checkout如何与React JS配合使用. ? 用于测试用户名的实时演示沙箱PayPal凭据:[email protected]密码:9lessons  Live D

node.js+express,实现RESTful API

node代码如下(exptest.js): var express = require('express'); var bodyParser = require('body-parser'); var app = express(); var patientinfo=require('./node_entity/patientinfo'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true }))

Express深入解读

本文基于Express 4.13.3. 一.使用Express 通常情况下,创建一个简单的服务器,代码如下: var http = require('http'); http.createServer(function(req, res) { res.write('hello world'); res.end(); }) .listen(4000); 如果使用Express,代码如下: var express = require('express'); var app = express();

拿nodejs快速搭建简单Oauth认证和restful API server攻略

拿nodejs快速搭建简单Oauth认证和restful API server攻略:http://blog.csdn.net/zhaoweitco/article/details/21708955 最近一直在鼓捣这个东西,拿出来分享下一下经验吧,其实很简单,一点也不难. 首先需求是这样,给自己的网站要增加API服务,API分为两种,公共的和私有授权的,授权的使用Oauth方法认证身份,API格式均为JOSN和JSONP. 嗯,别的语言我也没怎么学过,首先是找合适的框架进行实现吧.本身网站使用的e

使用Node.js + MongoDB 构建restful API

很多天前已经翻译了一大半了,今天收收尾~ RESTful API With Node.js + MongoDB Translated By 林凌灵 翻译目的:练练手,同时了解别人的思维方式 原文地址:RESTful API With Node.js + MongoDB 12 Sep 2013 我是一名移动应用开发者,我需要某种后端服务用来频繁地处理用户数据到数据库中.当然,我可以使用后端即服务类的网站(Parse, Backendless, 等等-),(译者:国内比较出名的有Bmob).但自己解