-
Notifications
You must be signed in to change notification settings - Fork 120
/
app.js
38 lines (31 loc) · 987 Bytes
/
app.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
var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');
var app = express();
var port = process.env.PORT || 1991;
app.use(bodyParser.json());
app.use(express.static(__dirname + '/src/'));
app.get('/', function (req, res) {
res.send('<script>location.href = "/demo";</script>');
});
// 接收post
app.post('/', function (req, res) {
var data = req.body;
if(data.base64.length !== data.size) {
return res.send({
error: '文件未接收完整,服务端生成失败'
});
}
var fileSrc = 'src/demo.jpg',
dataBuffer = new Buffer((data.base64).split(',')[1], 'base64');
fs.writeFile(fileSrc, dataBuffer, function (err) {
var file = fs.readFileSync(fileSrc);
res.send({
size: file.length,
src: req.url + 'demo.jpg?' + Date.now()
});
});
});
var server = app.listen(port, function () {
console.log('app running at ' + port);
});