-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
38 lines (31 loc) · 891 Bytes
/
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
var express = require('express');
var app = express();
var multer = require('multer');
var cors = require('cors');
app.use(cors());
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './public/uploads')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '-' + file.originalname)
}
})
app.get('/', function (req, res) {
res.send(req.file)
});
var upload = multer({ storage: storage }).single('myFile');
app.post('/', function(req, res) {
upload(req, res, function(err) {
console.log(err);
if (err instanceof multer.MulterError) {
return res.status(500).json(err)
} else if (err) {
return res.status(500).json(err)
}
return res.status(200).send(req.file)
})
});
app.listen(8000, function() {
console.log('Running on port 8000')
})