-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
157 lines (142 loc) · 4.66 KB
/
routes.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
157
const fs = require('fs')
const log = require('./utils')
const multiparty = require('multiparty')
const util = require('util')
// 引入 Model 模块
// 因为暴露出来的模块就是包含了 Model 的对象
// 所以分别将这个对象的属性提取出来赋值给相应的 model
const models = require('./models')
const User = models.User
const Customers = models.Customers
// 读取 html 文件的函数
const template = (name) => {
const path = 'templates/' + name
const options = {
encoding: 'utf8'
}
const content = fs.readFileSync(path, options)
return content
}
const index = (request) => {
const header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
let body = template('index.html')
const r = header + '\r\n' + body
return r
}
// 登录的处理函数, 根据请求方法来处理业务
const login = (request) => {
let result
if (request.method === 'POST') {
// 获取表单中的数据
const form = request.form()
// 根据 form 生成 User 实例
const u = User.create(form)
if (u.validateLogin()) {
const usa = User.all()
let valid = true
for (let i = 0; i < usa.length; i++) {
const us = usa[i]
if (us.username === u.username) {
valid = false
break
}
}
if (valid) {
u.save()
}
const header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
let body = template('Login-success.html')
const r = header + '\r\n' + body
return r
} else {
let body = template('login-false.html')
}
}
const header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
let body = template('login.html')
const r = header + '\r\n' + body
return r
}
const submitMes = (request) => {
const header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
let body = template('submit-message.html')
const r = header + '\r\n' + body
return r
}
const admin = (request) => {
ca = Customers.all()
let s = ''
for (let i = 0; i < ca.length; i++) {
s += `<h1>电话号:${ca[i].phoneNumbers} 订单数量:${ca[i].clothesNumbers}</h1>`
}
const header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
let body = template('admin.html')
body = body.replace('{{replace}}', s)
const r = header + '\r\n' + body
return r
}
const getCustomMessage = (request) => {
if (request.method === 'POST') {
// 获取表单中的数据
const form = request.form()
// 根据 form 生成 User 实例
const c = Customers.create(form)
if (c.validateLogin()) {
c.save()
}
}
const header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
let body = template('ok.html')
const r = header + '\r\n' + body
return r
}
// 接收发送过来的图片, 并保存到文件夹中
const uploadImg = (request) => {
const header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
const body = template('uploadImg.html')
const r = header + '\r\n' + body
return r
}
// 图片的响应函数, 读取图片并生成响应返回
const staticImg = (request) => {
// 静态资源的处理, 读取图片并生成相应返回
const filename = request.query.file || 'doge.gif'
const path = `static/img/${filename}`
const body = fs.readFileSync(path)
const header = 'HTTP/1.1 200 OK\r\nContent-Type: image/gif\r\n\r\n'
const h = Buffer.from(header)
const r = Buffer.concat([h, body])
return r
}
const staticJs = (request) => {
// 静态资源的处理, 读取文件并生成相应返回
const filename = request.query.file || ''
const path = `static/js/${filename}`
const body = fs.readFileSync(path)
const header = 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n'
const h = Buffer.from(header)
const r = Buffer.concat([h, body])
return r
}
const staticCss = (request) => {
// 静态资源的处理, 读取文件并生成相应返回
const filename = request.query.file || ''
const path = `static/css/${filename}`
const body = fs.readFileSync(path)
const header = 'HTTP/1.1 200 OK\r\nContent-Type: text/css\r\n\r\n'
const h = Buffer.from(header)
const r = Buffer.concat([h, body])
return r
}
// 把 route 放在一起, 然后暴露出去
const routeMapper = {
'/': index,
'/login': login,
'/static/img': staticImg,
'/static/js': staticJs,
'/static/css': staticCss,
'/submit-message': submitMes,
'/ok': getCustomMessage,
'/admin': admin
}
module.exports = routeMapper