express + restful

express

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);
});

 

 

restful api

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,可選):服務器能夠提供一些代碼或者腳本並在客戶的運行環境中執行。

   優勢:提升可擴展性

 

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

 

相關文章
相關標籤/搜索