crepecake
v4.0.0
Published
REST API server framework based on koa
Downloads
29
Maintainers
Readme
CrepeCake
REST API server framework based on koa.
Install
npm install crepecake
Getting started
example:
const CrepeCake = require('../index');
const HttpResponse = CrepeCake.HttpResponse;
const app = new CrepeCake();
const router = new CrepeCake.Router();
// -- Build your router
router.get('/', (ctx) => {
return 'ok';
});
router.post('/login', (ctx) => {
throw HttpResponse.notImplemented();
});
// -- Register middlewares
app.use(async function requestLogger(ctx, next) {
console.log(`${ctx.request.method} ${ctx.request.url}`);
await next();
});
app.use(router);
// -- Run
app.listen(3000, () => {
console.log('CrepeCake server running...');
});
Difference with Koa
Routing
You don't need to importkoa-router
separately, crepecake itself exposes aRouter
class, which has the same interface as koa router.
And you can directly mount a router onto another one, without doingrouteA.use(routeB.routes())
.Response parsing
In Koa, you need to doctx.body = 'hello, world'
in order to send a response. With crepecake, you can just return an object or an instance ofHttpResponse
from an async middleware function, crepecake will handle the response parsing properly and reply to the client.Error handling
Some shortcuts for throwing server errors are provided such likeHttpResponse.internalError
for your convenience. But you can still throw an error at anytime, the normal error handling procedure of Koa will be used in that case.