forked from papertiger8848/modelproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicUsageDemo.js
63 lines (53 loc) · 1.72 KB
/
basicUsageDemo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var app = require( 'express' )();
var ModelProxy = require( '../index' );
// 初始化modelproxy接口文件
ModelProxy.init( './interface_demo.json' );
// 配置拦截器,浏览器端可通过访问 127.0.0.1/model/[interfaceId]
app.use( '/model/', ModelProxy.Interceptor );
app.get( '/index', function( req, res ) {
res.sendfile( 'modelproxy-client.html' );
} );
var model = ModelProxy.create( 'Test.post' );
model.post( {
a: 'abc',
b: 'bcd',
c: '{"a":"b"}'
} ).done( function( data ) {
console.log( data );
} );
app.get( '/getCombinedData', function( req, res ) {
var searchModel = ModelProxy.create( 'Search.*' );
searchModel.list( { q: 'ihpone6' } )
.list( { q: '冲锋衣' } )
.suggest( { q: 'i' } )
.getNav( { q: '滑板' } )
.done( function( data1, data2, data3, data4 ) {
res.send( {
"list_ihpone6": data1,
"list_冲锋衣": data2,
"suggest_i": data3,
"getNav_滑板": data4
} );
} );
} );
// 带cookie的代理请求与回写
app.get( '/getMycart', function( req, res ) {
var cookie = req.headers.cookie;
console.log( 'request cookie:\n', cookie );
var cart = ModelProxy.create( 'Cart.*' );
cart.getMyCart()
.withCookie( cookie )
.done( function( data , setCookies ) {
console.log( 'response cookie:\n', setCookies );
res.setHeader( 'Set-Cookie', setCookies );
res.send( data );
}, function( err ) {
res.send( 500, err );
} );
} );
// 配置资源路由
app.get( '/modelproxy-client.js', function( req, res ) {
res.sendfile( './modelproxy-client.js' );
} );
app.listen( 3000 );
console.log( 'port: 3000' );