forked from quasarframework/quasar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (42 loc) · 1.24 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
const express = require('express')
const app = express()
const formidable = require('formidable')
const path = require('path')
const rimraf = require('rimraf')
const fs = require('fs')
const throttle = require('express-throttle-bandwidth')
const
port = process.env.PORT || 4444,
folder = path.join(__dirname, 'files')
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder)
}
else {
rimraf.sync(path.join(folder, '*'))
}
process.on('exit', () => {
rimraf.sync(path.join(folder))
})
app.set('port', port)
app.use(throttle(1024 * 128))
app.use((_, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.post('/upload', (req, res) => {
rimraf.sync(path.join(folder, '*'))
const form = new formidable.IncomingForm()
form.uploadDir = folder
form.parse(req, (_, fields, files) => {
console.log('\n-----------')
console.log('Fields', fields)
console.log('Received:', Object.keys(files))
console.log()
})
res.send('Thank you')
})
app.listen(port, () => {
console.log('\nUpload server running on http://localhost:' + port + '/upload')
console.log('You can now upload from main dev server using QUploader')
})