This repository has been archived by the owner on Mar 19, 2020. It is now read-only.
forked from javascriptru/course-nodejs-project
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
37 lines (32 loc) · 1.32 KB
/
app.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
const Koa = require('koa');
const config = require('./config');
const path = require('path');
const send = require('koa-send');
const Router = require('@koa/router');
const app = new Koa();
app.context.db = require('./libs/db');
app.log = require('./libs/log')();
app.use(require('koa-static')(config.publicRoot));
app.use(require('koa-bodyparser')());
require('./handlers/requestId')(app);
require('./handlers/requestLog')(app);
require('./handlers/nocache')(app);
require('./handlers/error')(app);
// uncomment to enable CORS from anywhere
app.use(require('@koa/cors')({maxAge: 86400}));
app.use(async (ctx, next) => {
if (!ctx.url.includes('.') && !ctx.url.startsWith('/api')) {
await send(ctx, 'index.html', {root: config.publicRoot});
} else {
await next();
}
});
const router = new Router({prefix: '/api'});
router.get('/dashboard/orders', require('./controllers/api/dashboard/orders'));
router.get('/dashboard/sales', require('./controllers/api/dashboard/sales'));
router.get('/dashboard/customers', require('./controllers/api/dashboard/customers'));
router.get('/dashboard/bestsellers', require('./controllers/api/dashboard/bestsellers'));
router.get('/reload', require('./controllers/api/reload'));
router.use('/rest', require('./controllers/api/rest')(app.context.db));
app.use(router.routes());
module.exports = app;