-
Notifications
You must be signed in to change notification settings - Fork 0
/
express_demo.js
158 lines (136 loc) · 4.23 KB
/
express_demo.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
158
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
var cookieParser = require('cookie-parser');
var fs = require('fs');
var data = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
helloworld
</body>
</html>
`;
// app.get('/', function(req, res) {
// res.send('hello world');
// });
// app.get('/index.html', function(req, res) {
// console.log(req.get('Cookie'));
// res.append('a', '1111');
// // res.type('application/json');
// // res.json({ "a": "haha", "b": "ahah" });
// // res.send({ "a": "haha", "b": "ahah" });
// // res.send(data);
// });
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.use(express.static('public'));
app.use(multer({ dest: '/tmp/' }).array('image'));
app.use(cookieParser());
// 主页输出 "Hello World"
app.get('/', function(req, res) {
console.log("主页 GET 请求");
console.log("Cookies: ", req.cookies)
res.send('Hello GET');
})
// POST 请求
app.post('/', function(req, res) {
console.log("主页 POST 请求");
res.send('Hello POST');
})
// /del_user 页面响应
app.get('/del_user', function(req, res) {
console.log("/del_user 响应 DELETE 请求");
res.send('删除页面');
})
// /list_user 页面 GET 请求
app.get('/list_user', function(req, res) {
console.log("/list_user GET 请求");
res.send('用户列表页面');
})
// 对页面 abcd, abxcd, ab123cd, 等响应 GET 请求
app.get('/ab*cd', function(req, res) {
console.log("/ab*cd GET 请求");
res.send('正则匹配');
})
app.get('/id/:num', function(req, res) { // http://localhost:8081/id/1
res.send('id');
})
app.get('/index.html', function(req, res) {
console.log("/ab*cd GET 请求");
res.sendFile(__dirname + '/' + 'index.html');
})
app.get('/process_get', function(req, res) { // http://127.0.0.1:8081/process_get?first_name=111&last_name=2222
// 输出 JSON 格式
var response = {
"first_name": req.query.first_name,
"last_name": req.query.last_name
};
console.log(response);
res.end(JSON.stringify(response));
})
app.post('/process_post', urlencodedParser, function(req, res) {
// 输出 JSON 格式
var response = {
"first_name": req.body.first_name,
"last_name": req.body.last_name
};
console.log(response);
res.end(JSON.stringify(response));
})
app.post('/file_upload', function(req, res) {
console.log(req.files[0]); // 上传的文件信息
var des_file = __dirname + "/upload/" + req.files[0].originalname;
fs.readFile(req.files[0].path, function(err, data) {
fs.writeFile(des_file, data, function(err) {
if (err) {
console.log(err);
} else {
response = {
message: 'File uploaded successfully',
filename: req.files[0].originalname
};
}
console.log(response);
res.end(JSON.stringify(response));
});
});
})
//添加的新用户数据
var user = {
"user4": {
"name": "mohit",
"password": "password4",
"profession": "teacher",
"id": 4
}
}
app.get('/addUser', function(req, res) {
// 读取已存在的数据
fs.readFile(__dirname + "/" + "users.json", 'utf8', function(err, data) {
data = JSON.parse(data);
data["user4"] = user["user4"];
console.log(data);
res.end(JSON.stringify(data));
});
})
app.get('/:id', function(req, res) {
// 首先我们读取已存在的用户
fs.readFile(__dirname + "/" + "users.json", 'utf8', function(err, data) {
data = JSON.parse(data);
var user = data["user" + req.params.id]
console.log(user);
res.end(JSON.stringify(user));
});
})
var server = app.listen(8081, function() {
var host = server.address().address;
var port = server.address().port;
console.log("应用实例,访问地址为 http://%s:%s", host, port);
});