-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkoa.js
35 lines (28 loc) · 969 Bytes
/
koa.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
const Koa = require('koa');
const Router = require('koa-router');
const body = require('koa-body');
const conditional = require('koa-conditional-get');
const serve = require('koa-static');
const Bundler = require('parcel-bundler');
const Path = require('path');
const { parcel: parcelConfig, static: staticConfig } = require('./config');
const generateToBase64 = require('./generate');
const router = new Router();
router.post('/generate', body(), async (ctx) => {
const options = ctx.request.body;
try {
ctx.body = await generateToBase64(options);
} catch (err) {
ctx.body = err;
ctx.status = 400;
}
});
const app = new Koa();
app.use(router.routes());
app.use(conditional());
app.use(serve(parcelConfig.outDir, staticConfig));
const port = process.env.PORT || 3000;
app.listen(port);
console.log(`Listening on port ${port}...`);
const parcelEntry = Path.join(__dirname, './app/index.html');
new Bundler(parcelEntry, parcelConfig).bundle();