-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
63 lines (52 loc) · 1.88 KB
/
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
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
const express = require('express')
const fileUpload = require('express-fileupload')
const app = express()
const bodyParser= require('body-parser')
app.set('port', process.env.PORT || 3000)
// default options
app.use( (req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader('Access-Control-Allow-Headers', 'my-header,X-Requested-With,content-type,Authorization,cache-control')
next()
})
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(fileUpload())
app.use(express.static(__dirname + "/public"))
const router = express.Router()
const error = ( err, res ) => {
console.log( err )
res.status(500).send('Error')
}
router.post('/upload', (req, res) => {
// Funcao sempre deve retornar algo
// console.log(req.files)
if (!req.files && !req.body.email) {
res.send('No files were uploaded.')
return false
}
if( (req.files.sampleFile.mimetype != 'application/pdf') &&
(req.files.sampleFile.mimetype != 'image/png') &&
(req.files.sampleFile.mimetype != 'image/jpeg') ){
res.send('File format not accepted')
return false
}
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
const sampleFile = req.files.sampleFile
const fileNameType = sampleFile.name
const fileType = fileNameType.substring( fileNameType.indexOf('.'), fileNameType.length)
const email = req.body.email
sampleFile.name = email.substring( 0, email.indexOf('@') ) +
`- ${Date.now()}${fileType}`
// Use the mv() method to place the file somewhere on your server
sampleFile.mv('./public/' + sampleFile.name, (err) =>
( err )
? error( err, res )
: res.send('File uploaded!'))
})
app.use( '/contax', router )
const server = app.listen( app.get( 'port' ), () => {
console.log( 'Server running at port: ' + server.address().port )
})