-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (48 loc) · 1.82 KB
/
server.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
var express = require('express');
var multiparty = require('multiparty');
var util = require('util');
var fs = require('fs');
var app = express();
app.use(express.static('example'));
app.use(express.static('dist'));
// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
res.send('hello world');
});
/* 上传*/
app.post('/file/uploading', function(req, res, next){
//生成multiparty对象,并配置上传目标路径
var form = new multiparty.Form({uploadDir: './server/temp/'});
//上传完成后处理
form.parse(req, function(err, fields, files) {
var filesTmp = JSON.stringify(files,null,2);
if(err){
console.log('parse error: ' + err);
} else {
console.log('parse files: ' + filesTmp);
var inputFile = files.file[0];
var uploadedPath = inputFile.path;
var dstPath = './server/files/' + inputFile.originalFilename;
//重命名为真实文件名
fs.rename(uploadedPath, dstPath, function(err) {
if(err){
console.log('rename error: ' + err);
} else {
console.log('rename ok');
}
});
}
res.writeHead(200, {'content-type': 'text/plain;charset=utf-8'});
//随机生成一个延迟
var timeout=Math.ceil(Math.random()*10)+3
setTimeout(function(){
res.end('{"success":true}');
},timeout*1000)
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
// module.exports = app;