|
|
|
const {Router} = require('express')
|
|
|
|
const pg = require('../db/pg')
|
|
|
|
|
|
|
|
const router = module.exports = new Router()
|
|
|
|
|
|
|
|
router.use((req, res, next)=>{
|
|
|
|
// Skip pretty-printing if in prod
|
|
|
|
if(req.app.locals.dev)
|
|
|
|
res.json = (val) => {
|
|
|
|
res.set('Content-Type', 'application/json');
|
|
|
|
res.end(JSON.stringify(val, null, 2))
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
|
|
|
router.use(require('cookie-session')({name: 'sos-session', secret: process.env.COOKIE_SECRET}))
|
|
|
|
router.use(require('./middleware/session'))
|
|
|
|
router.use('/auth', require('./auth'))
|
|
|
|
router.use('/cart', require('./cart'))
|
|
|
|
router.use('/users/', require('./users'))
|
|
|
|
router.use('/items/', require('./items'))
|
|
|
|
router.use('/images/', require('./images'))
|
|
|
|
router.use('/categories/', require('./categories'))
|
|
|
|
router.use('/orders/', require('./orders'))
|
|
|
|
router.use('/shipments/', require('./shipments'))
|
|
|
|
router.use('/email/', require('./email'))
|
|
|
|
|
|
|
|
router.use((req, res, next)=>{
|
|
|
|
const err = new Error('Not found')
|
|
|
|
err.status = 404
|
|
|
|
return next(err);
|
|
|
|
})
|
|
|
|
|
|
|
|
router.use((err, req, res, next)=> {
|
|
|
|
console.error(err.stack)
|
|
|
|
|
|
|
|
res.status(err.status || 500)
|
|
|
|
|
|
|
|
const error = {
|
|
|
|
message: err.message,
|
|
|
|
name: err.name
|
|
|
|
}
|
|
|
|
|
|
|
|
if(process.env.SOS_ENV === 'development')
|
|
|
|
error.stack = err.stack
|
|
|
|
|
|
|
|
res.json({ error })
|
|
|
|
})
|