0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Used Koa2 as Http Server in NodeJS

Last updated at Posted at 2017-10-25

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?