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 urlObject = url.parse(request.url);
|
31 |
|
32 |
|
33 | require('./tool/getData')(request, data => {
|
34 |
|
35 | let options = {
|
36 |
|
37 |
|
38 | method: request.method,
|
39 |
|
40 |
|
41 | url: urlObject.pathname.replace(/^\//, '').replace(/\/$/, ''),
|
42 |
|
43 |
|
44 | query: require('./tool/toQuery.js')(urlObject.query),
|
45 |
|
46 |
|
47 | value: data
|
48 |
|
49 | };
|
50 |
|
51 |
|
52 | let preUrl = options.url.split('/')[0];
|
53 |
|
54 | let contentType = 'application/json';
|
55 | let responseCode = '200';
|
56 | let responseData = JSON.stringify(options);
|
57 |
|
58 |
|
59 |
|
60 | if (preUrl == 'update') {
|
61 |
|
62 | let datapath = fullPath("./mock-" + urlToString(options.query.url, options.query.method) + ".js", mockBasePath);
|
63 |
|
64 |
|
65 | fs.writeFileSync(datapath, `module.exports=function(Mock){
|
66 | return ${options.value};
|
67 | };`);
|
68 |
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | else if (preUrl == 'delete') {
|
74 |
|
75 | let datapath = fullPath("./mock-" + urlToString(options.query.url, options.query.method) + ".js", mockBasePath);
|
76 |
|
77 |
|
78 | if (fs.existsSync(datapath)) {
|
79 | fs.unlinkSync(datapath);
|
80 | }
|
81 |
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 | else if (preUrl == 'query') {
|
87 |
|
88 | let datapath = fullPath("./mock-" + urlToString(options.query.url, options.query.method) + ".js", mockBasePath);
|
89 |
|
90 | if (fs.existsSync(datapath)) {
|
91 | try {
|
92 | responseData = JSON.stringify(require(datapath)(require('mockjs')));
|
93 | } catch (e) {
|
94 | responseCode = "500";
|
95 | responseData = "" + e;
|
96 | contentType = 'text/plain';
|
97 | }
|
98 | } else {
|
99 | responseCode = "404";
|
100 | }
|
101 |
|
102 | }
|
103 |
|
104 |
|
105 | else {
|
106 |
|
107 |
|
108 | let filePath = fullPath(options.url == "" ? "index.html" : options.url, basePath);
|
109 |
|
110 |
|
111 | let dotName = /\./.test(filePath) ? filePath.match(/\.([^.]+)$/)[1] : "";
|
112 |
|
113 |
|
114 | if (dotName != "") contentType = mineTypes[dotName];
|
115 |
|
116 |
|
117 | if (fs.existsSync(filePath) && !fs.lstatSync(filePath).isDirectory()) {
|
118 | responseData = fs.readFileSync(filePath);
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 | else {
|
124 | responseCode = "404";
|
125 | responseData = ('template404' in config ? config.template404 : require('./tool/template404'))(responseFileList(filePath));
|
126 | contentType = "text/html";
|
127 | }
|
128 |
|
129 | }
|
130 |
|
131 | response.writeHead(responseCode, {
|
132 |
|
133 |
|
134 | "Access-Control-Allow-Origin": "*",
|
135 | "Access-Control-Allow-Headers": "*",
|
136 | "Access-Control-Allow-Methods": "*",
|
137 |
|
138 |
|
139 | "X-Powered-By": jsonfile.name + " " + jsonfile.version,
|
140 |
|
141 |
|
142 | "Content-Type": contentType + ";charset=utf-8"
|
143 |
|
144 | });
|
145 |
|
146 | response.write(responseData);
|
147 |
|
148 | response.end();
|
149 |
|
150 | });
|
151 |
|
152 | })
|
153 |
|
154 |
|
155 | .listen(port);
|
156 |
|
157 | log(jsonfile.name + ' running on port:' + port);
|
158 |
|
159 | }; |
\ | No newline at end of file |