-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathform.js
69 lines (48 loc) · 1.5 KB
/
form.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
/*
* day 2
* FORM: npm i -S formidable 中间件
*/
'use strict'
const http = require("http");
const fs = require("fs");
const qs = require("querystring");
const formidable = require('formidable');
const util = require('util');
let res_ = (res,page) => fs.readFile(page, (err,data) => {
if (err) throw err;
res.writeHead(200, {"Content-Type": "text/html;charset=UFT-8"});
res.end(data);
});
http.createServer((req,res) => {
if(req.url == "/") {
res_.call(this,res,"./day2/index.html")
}else {
if(req.url == "/admin" && req.method == "POST") {
// parse a file upload
let form = new formidable.IncomingForm();
form.encoding = 'utf-8';
fs.access('./day2/upload',(err) => {
if (err) {
fs.mkdirSync("./day2/upload");
}
})
form.uploadDir = "./day2/upload";
form.parse(req, function(err, fields, files) {
console.log(files.file.name);
// 文件改名 新名称必须加上路劲
if(files.file.name) {
fs.rename(`./${files.file.path}`,`./day2/upload/${files.file.name}`,(err) => {
if(err) console.log(err);
});
}
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files})); // 工具类
});
return;
}
}
}).listen(3000,(err) => {
if (err) throw err;
console.log('serer is starting on port 3000 O(∩_∩)O哈哈~');
});