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 | } else {
|
114 | responseCode = "404";
|
115 | }
|
116 |
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 | else if (preUrl == 'handler') {
|
122 |
|
123 | let resultData = config.handler(options);
|
124 |
|
125 | if ('code' in resultData) responseCode = resultData.code;
|
126 | responseData = resultData.data;
|
127 | if ('type' in resultData) contentType = resultData.type;
|
128 |
|
129 | }
|
130 |
|
131 |
|
132 | else {
|
133 |
|
134 |
|
135 | let filePath = fullPath(options.url == "" ? "index.html" : options.url, basePath);
|
136 |
|
137 |
|
138 | let dotName = /\./.test(filePath) ? filePath.match(/\.([^.]+)$/)[1] : "";
|
139 |
|
140 |
|
141 | if (dotName != "") contentType = mineTypes[dotName];
|
142 |
|
143 |
|
144 | if (fs.existsSync(filePath) && !fs.lstatSync(filePath).isDirectory()) {
|
145 | responseData = fs.readFileSync(filePath);
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 | else {
|
151 | responseCode = "404";
|
152 | responseData = ('template404' in config ? config.template404 : require('./tool/template404'))(responseFileList(filePath));
|
153 | contentType = "text/html";
|
154 | }
|
155 |
|
156 | }
|
157 |
|
158 | }
|
159 | }
|
160 |
|
161 |
|
162 | catch (e) {
|
163 | responseCode = "500";
|
164 | responseData = "" + e;
|
165 | contentType = 'text/plain';
|
166 | }
|
167 |
|
168 | response.writeHead(responseCode, {
|
169 |
|
170 |
|
171 | "Access-Control-Allow-Origin": "*",
|
172 | "Access-Control-Allow-Headers": "*",
|
173 | "Access-Control-Allow-Methods": "*",
|
174 |
|
175 |
|
176 | "X-Powered-By": jsonfile.name + " " + jsonfile.version,
|
177 |
|
178 |
|
179 | "Content-Type": contentType + ";charset=utf-8"
|
180 |
|
181 | });
|
182 |
|
183 | response.write(responseData);
|
184 |
|
185 | response.end();
|
186 |
|
187 | });
|
188 |
|
189 | })
|
190 |
|
191 |
|
192 | .listen(port);
|
193 |
|
194 | log(jsonfile.name + ' running on port:' + port);
|
195 |
|
196 | }; |
\ | No newline at end of file |