-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
148 lines (127 loc) · 4.22 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// ps:使用之前请先阅读README.md文件
//
/****************************定义和引入********************************/
// 引入模块
var http = require('http');
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
var superagent = require('superagent'); //http://visionmedia.github.io/superagent/#response-properties 文档地址
var server = http.Server(app);
var host = 'http://localhost:';
var port = 9999; //设置本地转发服务端口
var Mock = require('mockjs')
/*************************以下为设置和启用*****************************/
// 设置node服务
app.set('port', port);
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
// 启动服务
server.listen(app.get('port'), function() {
console.log("服务已经启动,APIhost:" + host + port);
});
/******************以下为此服务支持跨域请求********************/
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS, PATCH');
res.set({
'Content-Type': 'Content-Type:application/json; charset=UTF-8',
})
if (req.method == 'OPTIONS') {
res.send(200);
} else {
next();
}
});
/******************以下是api公共部分,不用修改***********************/
// form 请求
var form = function(req, res, API, log) {
var sreq = superagent.post(dbUrl + API);
sreq.type('form')
sreq.send(req.body);
sreq.pipe(res);
sreq.on('end', function() {
console.log(log);
});
}
// json 请求
var json = function(req, res, API, log) {
var sreq = superagent.post(dbUrl + API);
sreq.type('json')
sreq.send(req.body);
sreq.pipe(res);
sreq.on('end', function() {
console.log(log);
});
}
/******************以下是api私有部分,必须修改***********************/
// 设置后端服务器url
var dbUrl = 'http://test.com'; //这里写你的后端api地址
//需配置部分
// app.get('这里写node服务接受前端发送参数的地址', function(req, res) {
// var API = '这里写后端java公共api,当然你们后端是什么的随意,,这里只是举个例子。'
// var log = '这里写node服务输出logs';
// form(req, res, API, log)
// });
//以下是不同请求发送方式
// form请求示例
app.get('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
form(req, res, API, log)
});
app.post('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
form(req, res, API, log)
});
app.put('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
form(req, res, API, log)
});
app.patch('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
form(req, res, API, log)
});
app.delete('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
form(req, res, API, log)
});
// json请求示例
app.get('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
json(req, res, API, log)
});
app.post('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
json(req, res, API, log)
});
app.put('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
json(req, res, API, log)
});
app.patch('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
json(req, res, API, log)
});
app.delete('/api/machine/init', function(req, res) {
var API = '/api/v2/machine/init'
var log = '初始化机器';
json(req, res, API, log)
});
// mock数据示例
app.get('/goods/brand', function(req, res) {
var data = Mock.mock(["鹰牌","维他","盒装","瓶装","特意浓","汇源","正宗","易购","恒大冰泉","宝矿力水特宝矿力水特","友芝友","光明畅优果粒"])
res.send(JSON.stringify(data))
console.log('goodsbrand')
})