Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
RESTful API framework based on koa and bookshelf
Writing a RESTful API has never been so easy!
Koapi is a library for building RESTful APIs in a really simple way.
npm install koapi
posts
id | title | contents | created_at | updated_at |
---|---|---|---|---|
1 | Title | Contents | 2016-8-1 | 2016-8-1 |
comments
id | post_id | title | contents | created_at | updated_at |
---|---|---|---|---|---|
1 | 1 | Title | Comment | 2016-8-1 | 2016-8-1 |
const { Koapi, router, middlewares, model } = require('koapi')
const app = new Koapi();
/****************** Connect to database ******************/
model.connect({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
})
class Comment extends model.Base {
get tableName () { return 'comments' }
get hasTimestamps () { return true }
}
class Post extends model.Base {
get tableName () { return 'posts' }
get hasTimestamps () { return true }
comments () {
return this.hasMany(Comment);
}
}
/****************** Implement Routers ******************/
const comments = router.resource(Comment, {
collection: ctx => ctx.state.parents.post.comments()
setup (route) {
// method "crud" is a shortcut for "create", "read", "update" and "destroy"
// YOU CAN ALSO USE MIDDLEWARE in "create", "read", "update", "destroy"
route.create(async(ctx, next) => {
// you can do anything before create
await next();
// you can do anything after create
})
route.read(/* You can place any middleware here if you need */{
filterable: ['created_at'], // filterable fields
sortable: ['created_at'], // sortable fields
});
route.destroy()
}
})
// POST /posts
// GET /posts
// GET /posts/:id
// PATCH /posts/:id
// DELETE /posts/:id
const posts = router.resource(Post, route => route.crud()).children(comments)
/****************** Run server ******************/
app.use(middlewares.preset('restful'))
app.use(middlewares.routers([ posts ]))
app.listen(3000);
node ./app
You have done your RESTful APIs in ONE minute
Checkout Koapp for your situation.
FAQs
RESTful API framework based on koajs
The npm package koapi receives a total of 31 weekly downloads. As such, koapi popularity was classified as not popular.
We found that koapi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.