2019-07-19 13:51:57 -07:00
|
|
|
//Allow user of @ in in require calls. Config in package.json
|
|
|
|
require('module-alias/register')
|
|
|
|
|
|
|
|
let Auth = require('@helpers/Auth')
|
|
|
|
|
|
|
|
const express = require('express')
|
|
|
|
const app = express()
|
|
|
|
const port = 3000
|
|
|
|
|
|
|
|
//Enable json body parsing in requests. Allows me to post data in ajax calls
|
|
|
|
app.use(express.json())
|
|
|
|
|
|
|
|
//Prefix defied by route in nginx config
|
|
|
|
const prefix = '/api'
|
|
|
|
|
|
|
|
//App Auth, all requests will come in with a token, decode the token and set global var
|
|
|
|
app.use(function(req, res, next){
|
|
|
|
|
|
|
|
let token = req.headers.authorization
|
|
|
|
if(token && token != null && typeof token === 'string'){
|
|
|
|
Auth.decodeToken(token)
|
|
|
|
.then(userData => {
|
|
|
|
|
|
|
|
req.headers.userId = userData.id //Update headers for the rest of the application
|
|
|
|
next()
|
|
|
|
}).catch(error => {
|
|
|
|
|
|
|
|
res.statusMessage = error //Throw 400 error if token is bad
|
|
|
|
res.status(400).end()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
next() //No token. Move along.
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
//Test
|
|
|
|
app.get(prefix, (req, res) => res.send('The api is running'))
|
|
|
|
|
|
|
|
//Init user endpoint
|
2019-07-30 12:21:12 -07:00
|
|
|
var user = require('@routes/userController')
|
2019-07-19 13:51:57 -07:00
|
|
|
app.use(prefix+'/user', user)
|
|
|
|
|
|
|
|
//Init notes endpoint
|
2019-07-30 12:21:12 -07:00
|
|
|
var notes = require('@routes/noteController')
|
|
|
|
app.use(prefix+'/note', notes)
|
2019-07-19 13:51:57 -07:00
|
|
|
|
|
|
|
//Init tags endpoint
|
2019-07-30 12:21:12 -07:00
|
|
|
var tags = require('@routes/tagController')
|
|
|
|
app.use(prefix+'/tag', tags)
|
2019-07-19 13:51:57 -07:00
|
|
|
|
|
|
|
//Output running status
|
|
|
|
app.listen(port, () => console.log(`Listening on port ${port}!`))
|