1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const http = require('http');
|
12 | const log = require('@hai2007/nodejs').log;
|
13 | const fs = require('fs');
|
14 | const path = require('path');
|
15 | const Mock = require('mockjs')
|
16 |
|
17 | const jsonfile = JSON.parse(fs.readFileSync(path.join(__dirname, './package.json')));
|
18 |
|
19 | module.exports = function (config) {
|
20 |
|
21 | const port = 'port' in config ? config.port : 8080;
|
22 |
|
23 | http.createServer(function (request, response) {
|
24 |
|
25 | let resultData = config.handler({
|
26 |
|
27 |
|
28 | method: request.method,
|
29 |
|
30 |
|
31 | url: (request.url + "").replace(/\?[^?]+$/, '')
|
32 |
|
33 | }, Mock);
|
34 |
|
35 | response.writeHead(resultData.status, {
|
36 |
|
37 |
|
38 | "Access-Control-Allow-Origin": "*",
|
39 | "Access-Control-Allow-Headers": "*",
|
40 | "Access-Control-Allow-Methods": "*",
|
41 |
|
42 |
|
43 | "X-Powered-By": jsonfile.name + " " + jsonfile.version,
|
44 |
|
45 |
|
46 | "Content-Type": "application/json;charset=utf-8"
|
47 |
|
48 | });
|
49 |
|
50 | response.write(require('@hai2007/tool').isString(resultData.data) ? resultData.data : JSON.stringify(resultData.data));
|
51 |
|
52 | response.end();
|
53 |
|
54 | })
|
55 |
|
56 |
|
57 | .listen(port);
|
58 |
|
59 | log(jsonfile.name + ' running on port:' + port);
|
60 |
|
61 | }; |
\ | No newline at end of file |