Moon Light Box

Time is Money

Koa2 - How to Use It?

| Comments

1. New Add Server.js and index.html(You can put it on folder "dist")

const fs = require('fs');
const path = require('path');
const Koa = require('koa');
const logger = require('koa-logger');
const serve = require('koa-static');
const compress = require('koa-compress');
const Router = require('koa-router');

const app = new Koa();

// https://github.com/koajs/static
const SERVE_OPTIONS = {
    maxage: 30 * 24 * 60 * 60 * 1000
};

const distFolder = path.resolve(__dirname, 'dist');

const root = new Router();
root.get('*', async (ctx) => {
    ctx.type = 'html';
    ctx.body = fs.createReadStream(`${distFolder}/index.html`);
});

const router = new Router();
router.use('/', root.routes(), root.allowedMethods());

app.use(logger());
app.use(compress());
app.use(serve(buildFolder, SERVE_OPTIONS));
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(80);

2. Execute it :)

# node Server.js

3. Browse http://127.0.0.1

koa2

Comments

comments powered by Disqus