
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
This is a simple restful api generator with express & mongoose.
$ npm i restal -S
// connect to mongodb and create a mongoose model
const mongoose = require('mongoose')
mongoose.Promise = Promise
mongoose.connect('mongodb://localhost/test', { useMongoClient: true })
const Post = new mongoose.Schema({
title: String,
content: { type: String, required: true },
classification: String
})
const post = mongoose.model('post', Post)
// create a express app
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json())
// import restal
const Restal = require('restal')
// create a restal instance with a model, and mount it on a uri
const postApis = new Restal(post, '/post')
// inject the apis into the express app
postApis.inject(app)
// listening
app.listen(8000)
constructor | remark |
---|---|
Restal(model, uri) | create a restal api instance |
field | remark |
---|---|
uri | the uri that restal instance mounted |
model | the model that restal instance associated |
method | remark |
---|---|
inject(app) | inject the apis into the express app |
preHandle(method, handler) | unshift pre-hanlde middleware(s) |
postHandle(method, handler) | push post-hanlde middleware(s) |
APIs are generated by restal when the restal instance created. And the APIs support five HTTP methods listed below.
method | query params | json body | remark |
---|---|---|---|
GET | id, cond, skip, num | (none) | get document(s) |
POST | (none) | document | create document |
PUT | id | document | update the whole document |
PATCH | id | some document props | update some document props |
DELETE | id, cond | (none) | delete document(s) |
After injection, APIs are routed by the express app and become active when the app is listening. The request body type of POST, PUT and PATCH is json and all responses are in json. So, the express app need to use body-parser before being injected.
query params | type | requirement | remark |
---|---|---|---|
id | ObjectID(_id) | optional | highest priority, ignore other params |
cond | json | optional | query object for mongodb, see mongodb manual |
skip | number | optional | number of documents skipped, default 0 |
num | number | optional | number of documents requested, default all |
No request body is needed. If all query params are missing, all documents of the collection will be responsed.
No query params needed but request body (the document object to create) is required in json.
query params | type | requirement | remark |
---|---|---|---|
id | ObjectID(_id) | required | locate the document needed to be updated |
The request body (the document object to update) is also required in json. The offered document will overwrite the whole original document in mongodb.
query params | type | requirement | remark |
---|---|---|---|
id | ObjectID(_id) | required | locate the document needed to be updated |
The request body (some document props to update) is also required in json. The offered document props will overwrite those of original document in mongodb.
query params | type | requirement | remark |
---|---|---|---|
id | ObjectID(_id) | required | highest priority, ignore "cond" param |
cond | json | optional | query object for mongodb, see mongodb manual |
No request body is needed. If all query params are missing, all documents of the collection will be deleted.
Features of express middleware are fully maintained in restal, and it's convenient to extend the restal instance handlers.
// import restal
const Restal = require('restal')
// create a restal instance with a model, and mount it on a uri
const postApis = new Restal(post, '/post')
// ----+----+----+---------+----+----+----
// preHandle -> ... | f2 | f1 | handler | f1 | f2 | ... <- postHandle
// ----+----+----+---------+----+----+----
// add middleware(s) to pre-hanlde or intercept the request
postApis.preHandle('get', (req, res, next) => {
const postModel = postApis.model
const postUri = postApis.uri
// do something here like authorization check
// if next misses, original handler will be blocked
// and you need to reponse on your own
next()
})
// add middleware to post-handle the result
postApis.postHandle('get', (result, req, res, next) => {
const postModel = postApis.model
const postUri = postApis.uri
// do somethind here like data processing
// if you push post handler, the orignal handler won't
// reponse the request and pass the orignal reponse to
// post handler with param "result"
// you need to reponse on your own
res.status(200).send(result)
})
// inject the apis into the express app
postApis.inject(app)
FAQs
A Express restful api generator with Mongoose
We found that restal 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
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.