- 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
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
import Koa from 'koa'
import { Nuxt, Builder } from 'nuxt'
import cors from 'kcors'
import koaJwt from 'koa-jwt'
import Router from 'koa-router'
import koaBody from 'koa-body'
import * as api from './routes/api'
// Use this middleware on routes that require
// token validation. Remember that if valid,
// it will set `ctx.state.user` to decoded data.
const jwt = koaJwt({
secret: 'sec3t--key!!', // Should not be hardcoded
});
const app = new Koa()
const router = new Router()
const koabody = new koaBody({ multipart: true })
const host = process.env.HOST || '127.0.0.1'
const port = 8080 //process.env.PORT || 8080
// Import and Set Nuxt.js options
let config = require('../nuxt.config.js')
config.dev = !(app.env === 'production')
const nuxt = new Nuxt(config)
if (config.dev) {
const builder = new Builder(nuxt)
builder.build().catch(e => {
console.error(e) // eslint-disable-line no-console
process.exit(1)
})
}
app.use(async (ctx, next) => {
await next()
ctx.status = 200 // koa defaults to 404 when it sees that status is unset
return new Promise((resolve, reject) => {
ctx.res.on('close', resolve)
ctx.res.on('finish', resolve)
nuxt.render(ctx.req, ctx.res, promise => {
// nuxt.render passes a rejected promise into callback on error.
promise.then(resolve).catch(reject)
})
})
})
//app.use(router.routes())
// User
router.post('/api/user/login', koabody, api.user.login)
router.post('/api/user/register', koabody, api.user.register)
// Addresses
router.get('/api/user/addresses', jwt, api.user.getAddresses)
router.post('/api/user/address', koabody, jwt, api.user.addAddress)
router.put('/api/user/address', koabody, jwt, api.user.updateAddress)
router.delete('/api/user/address', jwt, api.user.deleteAddress)
// Credit Cards
router.get('/api/user/cards', jwt, api.user.getAllCreditCards)
router.post('/api/user/cards', koabody, jwt, api.user.addCreditCard)
router.delete('/api/user/cards', jwt, api.user.deleteCreditCard)
// Orders
router.get('/api/user/orders', jwt, api.user.orders)
//router.post('/api/user/cards', koabody, jwt, api.user.addCreditCard)
//router.delete('/api/user/cards', jwt, api.user.deleteCreditCard)
// Cart
router.post('/api/cart/checkout', koabody, jwt, api.cart.checkout)
// app.use(async (ctx, next) => {
// await next()
// ctx.status = 200 // koa defaults to 404 when it sees that status is unset
// return new Promise((resolve, reject) => {
// ctx.res.on('close', resolve)
// ctx.res.on('finish', resolve)
// nuxt.render(ctx.req, ctx.res, promise => {
// // nuxt.render passes a rejected promise into callback on error.
// promise.then(resolve).catch(reject)
// })
// })
// })
app.listen(port, host)