-
Notifications
You must be signed in to change notification settings - Fork 12
/
nodeServer.js
156 lines (141 loc) · 4.99 KB
/
nodeServer.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
148
149
150
151
152
153
154
155
156
const http = require('http')
const fs = require('fs');
const path = require('path');
const beautify = require('js-beautify').js
// querystring 模块提供用于解析和格式化 URL 查询字符串的实用工具
const querystring = require('querystring')
const promise = (fn) => new Promise(fn)
const mkdirCreateFile = async (codePath, content, lastPath) => {
const reg = /\\/g;
codePath = codePath.replace(reg, '/')
// 截取目录
const pathArr = codePath.split('/');
let mkdirPath = ''
lastPath = lastPath || '';
if (pathArr.length > 1) {
if (!lastPath) {
mkdirPath = pathArr.shift();
lastPath = mkdirPath;
} else {
mkdirPath = pathArr.shift();
lastPath = `${lastPath}/${mkdirPath}`;
mkdirPath = lastPath;
}
await promise((resolve, reject) => {
// 创建目录
fs.mkdir(path.join(__dirname, mkdirPath), async function (err) {
// 递归创建目录
resolve();
})
}).then(async () => {
await mkdirCreateFile(pathArr.join('/'), content, lastPath)
})
} else {
mkdirPath = pathArr.shift();
mkdirPath = `${lastPath}/${mkdirPath}`;
await promise((resolve, reject) => {
// 写入文件
fs.writeFile(path.join(__dirname, mkdirPath), content, async function (err) {
if (err) {
// console.log('写入失败', err);
reject(err)
} else {
resolve()
// console.log('写入成功');
}
})
})
}
}
// 路由配置
const routes = {
createCode: {
url: '/createCode',
callback: function (request, response) {
// 接收数据
let postData = ''
// chunk为一点点数据,逐渐积累
request.on('data', chunk => {
// console.log('chunk=', chunk)
postData += chunk
})
request.on('end', () => {
let {
path: codePath,
code
} = querystring.parse(postData.toString())
code=beautify(code, { indent_size: 4, space_in_empty_paren: true })
console.log('codePath=', codePath)
console.log('code=', code)
mkdirCreateFile(codePath, code).then(() => {
console.log('文件写入成功');
// 在这里返回 因为是异步
response.end(
// 返回json字字符串
JSON.stringify({
message:'文件写入成功',
path: codePath,
code
})
)
}).catch((err) => {
console.log('文件写入失败', err);
response.end(
// 返回json字字符串
JSON.stringify({
message: '文件写入失败'
})
)
})
})
},
type: 'post',
},
getCode: {
url: '/getCode',
callback: function (request, response) {
// url路径
const url = request.url
const path = url.split('?')[0]
// 解析 get请求的参数 为?后面 所以数组下标为1
const getParams = querystring.parse(url.split('?')[1])
response.end(
// 返回json字字符串
JSON.stringify({
...getParams
})
)
},
type: 'get',
}
}
const server = http.createServer((req, res) => {
// 请求的方式
const method = req.method
// 获取完整请求url
const url = req.url
//设置跨域
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
// res.setHeader("X-Powered-By", ' 3.2.1')
for (let key in routes) {
if (url.indexOf(routes[key].url) != '-1') {
// 设置返回的格式 json格式
res.setHeader('Content-type', 'application/json')
if (method === 'POST' && routes[key].type == 'post') { // 0.如果是Post请求
routes[key].callback(req, res)
} else if (method === 'GET' && routes[key].type == 'get') { // get请求
console.log('method', method)
routes[key].callback(req, res)
}
return false;
}
}
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('Hello World\n')
})
server.listen(5000, () => {
console.log('http://localhost:5000/')
})