-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
43 lines (36 loc) · 1.18 KB
/
index.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
const config = require('config')
const path = require('path')
const Koa = require('koa')
const Router = require('koa-router')
const loadRoutes = require("./app/routes")
const DataLoader = require('./app/dataLoader')
const views = require('koa-views')
const serve = require('koa-static')
const app = new Koa()
const router = new Router()
// Data loader for products (reads JSON files)
const productsLoader = new DataLoader(
path.join(
__dirname,
config.get('data.path'),
'products')
)
// Views setup, adds render() function to ctx object
app.use(views(
path.join(__dirname, config.get('views.path')),
config.get('views.options')
))
// Server static files (scripts, css, images)
app.use(serve(config.get('static.path')))
// Hydrate ctx.state with global settings, to make them available in views
app.use(async (ctx, next) => {
ctx.state.settings = config.get('settings')
ctx.state.urlWithoutQuery = ctx.origin + ctx.path
await next()
})
// Configure router
loadRoutes(router, productsLoader)
app.use(router.routes())
// Start the app
const port = process.env.PORT || config.get('server.port')
app.listen(port, () => { console.log(`Application started - listening on port ${port}`) })