https://github.com/danielwpz/crepecake
REST API server framework based on koa
https://github.com/danielwpz/crepecake
koajs nodejs rest-api
Last synced: 6 months ago
JSON representation
REST API server framework based on koa
- Host: GitHub
- URL: https://github.com/danielwpz/crepecake
- Owner: danielwpz
- License: mit
- Created: 2019-01-16T19:26:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-03-18T07:54:58.000Z (over 5 years ago)
- Last Synced: 2025-02-27T02:17:10.518Z (over 1 year ago)
- Topics: koajs, nodejs, rest-api
- Language: TypeScript
- Size: 474 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CrepeCake
REST API server framework based on koa.
## Install
`npm install crepecake`
## Getting started
example:
```javascript
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
1. Routing
You don't need to import `koa-router` separately, crepecake itself exposes a `Router` class, which has the same interface as koa router.
And you can directly mount a router onto another one, without doing `routeA.use(routeB.routes())`.
2. Response parsing
In Koa, you need to do `ctx.body = 'hello, world'` in order to send a response. With crepecake, you can just return an object or an instance of `HttpResponse` from an async middleware function, crepecake will handle the response parsing properly and reply to the client.
3. Error handling
Some shortcuts for throwing server errors are provided such like `HttpResponse.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.