Skip to content

Instantly share code, notes, and snippets.

@Industrial Industrial/server.js Secret
Last active Dec 15, 2016

Embed
What would you like to do?
import path from 'path'
import zlib from 'zlib'
import fs from 'fs'
import Koa from 'koa'
import koaStatic from 'koa-static'
import koaBodyparser from 'koa-bodyparser'
import koaCompress from 'koa-compress'
import koaConvert from 'koa-convert'
import koaHandleError from 'koa-handle-error'
import koaMount from 'koa-mount'
import koaPassport from 'koa-passport'
import koaRouter from 'koa-router'
import koaSession from 'koa-generic-session'
const CWD = path.resolve(__dirname)
const HOST = process.env.IP || '0.0.0.0'
const PORT = /*process.env.PORT ||*/ 3000
const ADDRESS = `http://${HOST}:${PORT}`
console.log()
console.log('CWD: ', CWD)
console.log('HOST: ', HOST)
console.log('PORT: ', PORT)
console.log('ADDRESS: ', ADDRESS)
console.log()
function errorHandler(error) {
console.error('KOA ERROR: ' + error.stack || error.message || error)
}
const router = koaRouter()
const app = new Koa()
app.keys = ['keyboardcat']
const _use = app.use
app.use = x => _use.call(app, koaConvert(x))
app
.use(koaHandleError(errorHandler))
.use(koaBodyparser())
.use(koaSession())
.use(koaPassport.initialize())
.use(koaPassport.session())
.use(router.routes())
.use(router.allowedMethods())
.use(koaStatic(`${CWD}/../client`))
.use(koaCompress({
filter: (contentType) => /text/i.test(contentType),
threshold: 2048,
flush: zlib.Z_SYNC_FLUSH,
}))
console.log('Starting...')
app
.listen(PORT, HOST, () => {
console.log('Started.')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.