-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 1.73 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require('express')
const fileUpload = require('express-fileupload')
const cors = require('cors')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const getAuth = require('./validateUser').getAuth
const app = express()
// enable files upload
app.use(fileUpload({
createParentPath: true
}))
//add other middleware
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(morgan('dev'))
app.use(express.static('uploads'))
//start app
const port = process.env.PORT || 7000
app.listen(port, () =>
console.log(`App is listening on port ${port}.`)
)
app.post('/upload-file', async (req, res) => {
try {
if (!req.files) {
res.send({
status: false,
message: 'No file uploaded'
})
} else {
//Use the name of the input field (i.e. 'audiofile') to retrieve the uploaded file
let audiofile = req.files.audiofile
let folder = req.body.folder || 'uploads'
let id = req.body.clientId
let secret = req.body.clientSecret
let issuer = req.body.issuer
let resource = req.body.resource
const auth = await getAuth(id, secret, issuer, resource)
//console.log(auth.status) // if status === 200 upload file
console.log(auth)
//Use the mv() method to place the file in the upload directory (i.e. 'uploads')
audiofile.mv('./' + folder + '/' + audiofile.name)
//send response
res.send({
status: true,
message: 'File is uploaded',
data: {
name: audiofile.name,
mimetype: audiofile.mimetype,
size: audiofile.size
}
})
}
} catch (err) {
res.status(500).send(err)
}
})