2017-04-18 05:52:09 +00:00
|
|
|
const path = require('path')
|
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const morgan = require('morgan')
|
2017-04-13 04:13:44 +00:00
|
|
|
|
2017-04-18 05:52:09 +00:00
|
|
|
const app = express()
|
2017-04-13 04:13:44 +00:00
|
|
|
|
2017-04-18 05:52:09 +00:00
|
|
|
const port = process.env.PORT || 8080
|
2017-04-13 04:13:44 +00:00
|
|
|
const welcome = 'Test images API at http://localhost:' + port
|
|
|
|
console.log(welcome)
|
|
|
|
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
app.use(morgan('dev'))
|
2017-04-18 05:52:09 +00:00
|
|
|
app.get('/', (req, res) => res.send(welcome))
|
|
|
|
app.listen(port)
|
2017-04-13 04:13:44 +00:00
|
|
|
|
|
|
|
const authentication = (req, res, next) => {
|
|
|
|
const token = req.query.token || req.headers['token']
|
|
|
|
if (token) {
|
|
|
|
next()
|
|
|
|
} else {
|
2017-04-18 05:52:09 +00:00
|
|
|
return res.status(403).send({ success: false })
|
2017-04-13 04:13:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const staticPictures = express.static(path.join(__dirname, 'pictures'))
|
|
|
|
|
|
|
|
app.use('/pictures', authentication, staticPictures)
|