express-query-defaults
v0.1.2
Published
Query filter
Downloads
2
Readme
express-query-defaults
npm install express-query-defaults --save
Usage
const defaults = require('express-query-defaults')
app.get(
'/posts',
defaults({
limit: { type: 'number', default: 30 }, // parse limit as number with default 30
offset: { type: 'number', default: 0 }, // parse offset as number with default 0
tags: { type: 'array', default: [] }, // parse coma-separated string as array
sort: { // sort: "asc" and "1" for 1, everything else if -1
type: 'string',
default: v => [ 'asc', '1' ].includes((v || '').toLowerCase()) ? 1 : -1
}
}),
(req, res, next) => {
const { limit, offset, sort, tags } = req.query
Post
.find()
.where({ tags: { $all: tags } })
.skip(offset)
.limit(limit)
.sort({ createdAt: sort })
.exec((err, posts) => {
res.json(posts)
})
}
)