@daniellehuisman/koa-base
v1.3.1
Published
Koa server with basic middleware
Downloads
5
Maintainers
Readme
koa-base
Koa server with basic middleware.
Middleware
- Koa Helmet
- Koa Response Time
- Koa Conditional GET
- Koa ETag
- Koa Compress
- Koa Session
- Koa Body Parser
- Koa JSON
- Koa Static
- Winston logger
Installation
yarn add @danielhuisman/koa-base
Usage
import {createServer, logger, startServer} from '@danielhuisman/koa-base';
import Router from 'koa-router';
import path from 'path';
const config = {
port: 5000,
session: {
secret: 'sessionSecret'
},
static: {
serve: process.env.STATIC_SERVE !== 'false',
path: path.join(__dirname, '..', 'static')
}
};
(async () => {
logger.info('Starting application...');
// Initialize server
const {server, app} = createServer(config);
// Initialize router
const router = new Router();
// Index route
router.get('/', async (ctx) => {
return ctx.success(
{
message: 'Hello World!'
},
200
);
});
// Add router
app.use(router.routes());
app.use(router.allowedMethods());
// Handle unknown routes
app.use(async (ctx, next) => {
ctx.error(404, 'Not found');
await next();
});
// Start server
await startServer(config, server);
logger.info('Started application.');
})();