1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const http = require('http');
|
12 | const { log, fullPath } = require('@hai2007/nodejs');
|
13 | const fs = require('fs');
|
14 | const path = require('path');
|
15 | const url = require('url');
|
16 | const mineTypes = require('./mime.types.js');
|
17 | const responseFileList = require('./tool/responseFileList.js');
|
18 | const urlToString = require('./tool/urlToString');
|
19 |
|
20 | const jsonfile = JSON.parse(fs.readFileSync(path.join(__dirname, './package.json')));
|
21 |
|
22 | module.exports = function (config) {
|
23 |
|
24 | const port = 'port' in config ? config.port : 8080;
|
25 | const basePath = fullPath(config.contentBase || "./", process.cwd());
|
26 | const mockBasePath = fullPath(config.mockBase || "./", process.cwd());
|
27 |
|
28 | http.createServer(function (request, response) {
|
29 |
|
30 | let contentType = 'application/json';
|
31 | let responseCode = '200';
|
32 | let responseData = "{}";
|
33 |
|
34 | let urlObject = url.parse(request.url);
|
35 |
|
36 |
|
37 | require('./tool/getData')(request, data => {
|
38 |
|
39 | let options = {
|
40 |
|
41 |
|
42 | method: request.method,
|
43 |
|
44 |
|
45 | url: urlObject.pathname.replace(/^\//, '').replace(/\/$/, ''),
|
46 |
|
47 |
|
48 | query: require('./tool/toQuery.js')(urlObject.query),
|
49 |
|
50 |
|
51 | value: data
|
52 |
|
53 | };
|
54 |
|
55 | try {
|
56 | if ('intercept' in config && !config.intercept(options)) {
|
57 | responseCode = "501";
|
58 | responseData = "The current request is not supported.";
|
59 | contentType = 'text/plain';
|
60 | } else {
|
61 |
|
62 |
|
63 | let preUrl = options.url.split('/')[0];
|
64 |
|
65 |
|
66 |
|
67 | if (preUrl == 'update') {
|
68 |
|
69 | let datapath = fullPath("./mock-" + urlToString(options.query.url, options.query.method) + ".js", mockBasePath);
|
70 |
|
71 |
|
72 | fs.writeFileSync(datapath, `module.exports=function(Mock){return ${options.value};};`, {
|
73 | encoding: 'utf8'
|
74 | });
|
75 |
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 | else if (preUrl == 'delete') {
|
81 |
|
82 | let datapath = fullPath("./mock-" + urlToString(options.query.url, options.query.method) + ".js", mockBasePath);
|
83 |
|
84 |
|
85 | if (fs.existsSync(datapath)) {
|
86 | fs.unlinkSync(datapath);
|
87 | }
|
88 |
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 | else if (preUrl == 'query') {
|
94 |
|
95 | let datapath = fullPath("./mock-" + urlToString(options.query.url, options.query.method) + ".js", mockBasePath);
|
96 |
|
97 | if (fs.existsSync(datapath)) {
|
98 | responseData = JSON.stringify(require(datapath)(require('mockjs')));
|
99 | } else {
|
100 | responseCode = "404";
|
101 | }
|
102 |
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 | else if (preUrl == 'oralquery') {
|
108 |
|
109 | let datapath = fullPath("./mock-" + urlToString(options.query.url, options.query.method) + ".js", mockBasePath);
|
110 |
|
111 | if (fs.existsSync(datapath)) {
|
112 | responseData = (fs.readFileSync(datapath, 'utf-8') + "").replace(/^module\.exports=function\(Mock\)\{return /, '').replace(/;\};$/, '');
|
113 | contentType = 'text/plain';
|
114 | } else {
|
115 | responseCode = "404";
|
116 | }
|
117 |
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 | else if (preUrl == 'handler') {
|
123 |
|
124 | let resultData = config.handler(options);
|
125 |
|
126 | if ('code' in resultData) responseCode = resultData.code;
|
127 | responseData = resultData.data;
|
128 | if ('type' in resultData) contentType = resultData.type;
|
129 |
|
130 | }
|
131 |
|
132 |
|
133 | else {
|
134 |
|
135 |
|
136 | let filePath = fullPath(options.url == "" ? "index.html" : options.url, basePath);
|
137 |
|
138 |
|
139 | let dotName = /\./.test(filePath) ? filePath.match(/\.([^.]+)$/)[1] : "";
|
140 |
|
141 |
|
142 | if (dotName != "") contentType = mineTypes[dotName];
|
143 |
|
144 |
|
145 | if (fs.existsSync(filePath) && !fs.lstatSync(filePath).isDirectory()) {
|
146 | responseData = fs.readFileSync(filePath);
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 | else {
|
152 | responseCode = "404";
|
153 | responseData = ('template404' in config ? config.template404 : require('./tool/template404'))(responseFileList(filePath));
|
154 | contentType = "text/html";
|
155 | }
|
156 |
|
157 | }
|
158 |
|
159 | }
|
160 | }
|
161 |
|
162 |
|
163 | catch (e) {
|
164 | responseCode = "500";
|
165 | responseData = "" + e;
|
166 | contentType = 'text/plain';
|
167 | }
|
168 |
|
169 | response.writeHead(responseCode, {
|
170 |
|
171 |
|
172 | "Access-Control-Allow-Origin": "*",
|
173 | "Access-Control-Allow-Headers": "*",
|
174 | "Access-Control-Allow-Methods": "*",
|
175 |
|
176 |
|
177 | "X-Powered-By": jsonfile.name + " " + jsonfile.version,
|
178 |
|
179 |
|
180 | "Content-Type": contentType + ";charset=utf-8"
|
181 |
|
182 | });
|
183 |
|
184 | response.write(responseData);
|
185 |
|
186 | response.end();
|
187 |
|
188 | });
|
189 |
|
190 | })
|
191 |
|
192 |
|
193 | .listen(port);
|
194 |
|
195 | log(jsonfile.name + ' running on port:' + port);
|
196 |
|
197 | }; |
\ | No newline at end of file |