-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
186 lines (182 loc) · 7.22 KB
/
server.js
File metadata and controls
186 lines (182 loc) · 7.22 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// server.js -- server-side JavaScript
// This file is the entry point for the server-side code.
// It is responsible for setting up the server and handling requests.
import http from 'node:http';
import path from 'node:path';
import url from 'node:url';
import fs from 'node:fs';
import formidable from 'formidable';
// default configuration
const config = {
"host": "localhost",
"port": 3001,
"root": "./",
"indexPage": "./html/index.html"
};
// MIME types
const mimeTypes = {
".html": "text/html",
".css": "text/css",
".js": "application/javascript",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
".ico": "image/x-icon",
".svg": "image/svg+xml",
".pdf": "application/pdf",
".zip": "application/zip",
".doc": "application/msword",
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".xls": "application/vnd.ms-excel",
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".txt" : "text/plain",
".mp3": "audio/mpeg",
".wav": "audio/wav",
".mp4": "video/mp4",
".webm": "video/webm",
".woff": "font/woff",
".woff2": "font/woff2",
".ttf": "font/ttf",
".eot": "application/vnd.ms-fontobject"
};
class Server {
constructor() {
this.host = config.host;
this.port = config.port;
this.root = config.root;
this.indexPage = config.indexPage;
}
// 处理 404
respondNotFound(req, res) {
res.writeHead(404, {'Content-Type': 'text/html'});
const readStream = fs.createReadStream(this.root + 'html/404.html');
readStream.on('error', (err) => {
console.error('Error reading 404 file:', err);
res.end('Not Found');
});
readStream.pipe(res);
// res.end('Not Found');
}
// 处理文件请求
respondFile(pathName, req, res) {
const ext = path.extname(pathName);
const mimeType = mimeTypes[ext] || 'application/octet-stream';
res.writeHead(200, {'Content-Type': mimeType});
const readStream = fs.createReadStream(pathName);
readStream.on('error', (err) => {
console.error('Error reading file:', err);
this.respondNotFound(req, res);
});
readStream.pipe(res);
}
/**
* 处理路由
* @param {string} pathName
* @param {http.IncomingMessage} req
* @param {http.ServerResponse<http.IncomingMessage> &
* {req: http.IncomingMessage;}} res
*/
routeHandler(pathName, req, res) {
if (req.method === 'POST' && req.url === '/upload') {
console.log('Uploading file...');
const uploadDir = path.join(this.root, 'uploads');
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir);
}
const form = formidable({
uploadDir: uploadDir,
keepExtensions: true
});
form.parse(req, (err, fields, files) => {
if (err) {
console.error('Error parsing form:', err);
res.writeHead(500, {'Content-Type': 'application/json'});
res.end(JSON.stringify({success: false, message: 'Error parsing form'}));
return;
} else {
console.log('File uploaded:', files);
const uploadFiles = Array.isArray(files.file)
? files.file.map((file) => ({
originalFilename: file.originalFilename,
newFilename: file.newFilename,
size: file.size,
filepath: file.filepath
}))
: [{
originalFilename: files.file.originalFilename,
newFilename: files.file.newFilename,
size: files.file.size,
filepath: files.file.filepath
}]
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({success: true, files: uploadFiles}));
return;
}
});
} else if (req.method === 'GET' && req.url.includes('/download')) {
const parsedUrl = url.parse(req.url, true);
const fileUrl = parsedUrl.query.fileUrl;
console.log('Downloading file:', fileUrl);
const readStream = fs.createReadStream(fileUrl);
readStream.on('error', (err) => {
console.error('Error reading file:', err);
this.respondNotFound(req, res);
});
res.setHeader('Content-Disposition', `attachment; filename=${path.basename(fileUrl)}`);
readStream.pipe(res);
} else if (req.method === 'DELETE' && req.url === '/delete') {
console.log('Deleting file...');
let data = '';
req.on('data', chunk => {
data += chunk;
});
req.on('end', () => {
const parsedData = JSON.parse(data);
const fileUrl = parsedData.fileUrl;
console.log('Deleting file:', fileUrl);
fs.unlink(fileUrl, (err) => {
if (err) {
console.error('Error deleting file:', err);
res.writeHead(500, {'Content-Type': 'application/json'});
res.end(JSON.stringify({success: false, message: 'Error deleting file'}));
} else {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({success: true, message: 'File deleted successfully'}));
}
});
});
} else {
fs.stat(pathName, (err, stats) => {
if (err) {
console.error('Error stat-ing file:', err);
this.respondNotFound(req, res);
} else if (stats.isDirectory()) {
this.routeHandler(path.join(pathName, this.indexPage), req, res);
} else if (stats.isFile()) {
this.respondFile(pathName, req, res);
} else {
this.respondNotFound(req, res);
}
});
}
}
// 启动服务器
start() {
http.createServer((req, res) => {
const parsedUrl = url.parse(req.url);
const pathName = path.join(this.root, path.normalize(parsedUrl.pathname));
console.log(`Request for ${pathName}`);
// res.statusCode = 200;
this.routeHandler(pathName, req, res);
}).listen(this.port, this.host, (err) => {
if (err) {
console.error('Error starting server:', err);
console.info('Exiting...');
} else {
console.log(`Server running at http://${this.host}:${this.port}/`);
}
})
}
}
const server = new Server();
server.start();