-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
49 lines (41 loc) · 1.59 KB
/
index.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
var express = require('express')
var multer = require('multer')
var port = 3000;
var app = express()
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
var upload = multer({ storage: storage })
/*
app.use('/a',express.static('/b'));
Above line would serve all files/folders inside of the 'b' directory
And make them accessible through http://localhost:3000/a.
*/
app.use(express.static(__dirname + '/public'));
app.use('/uploads', express.static('uploads'));
app.post('/profile-upload-single', upload.single('profile-file'), function (req, res, next) {
// req.file is the `profile-file` file
// req.body will hold the text fields, if there were any
console.log(JSON.stringify(req.file))
var response = '<a href="/">Home</a><br>'
response += "Files uploaded successfully.<br>"
response += `<img src="${req.file.path}" /><br>`
return res.send(response)
})
app.post('/profile-upload-multiple', upload.array('profile-files', 12), function (req, res, next) {
// req.files is array of `profile-files` files
// req.body will contain the text fields, if there were any
console.log(JSON.stringify(req.file))
var response = '<a href="/">Home</a><br>'
response += "Files uploaded successfully.<br>"
for(var i=0;i<req.files.length;i++){
response += `<img src="${req.files[i].path}" /><br>`
}
return res.send(response)
})
app.listen(port,() => console.log(`Server running on port ${port}!\nClick http://localhost:3000/`))