nongos
v0.5.3
Published
[![JS Standard Style][standard-image]][standard-url] [![NPM version][npm-image]][npm-url]
Readme
nongos
Project still under development!
An opinionated framework for fast prototyping RESTful APIs with Node.js based on the MEAN stack!
Install
npm i nongos -SBasic usage
const nongos = require('nongos')
nongos.resource(
'users',
{
name: {
type: String,
required: [true, 'required']
},
age: {
type: Number,
required: [true, 'required'],
min: [10, 'at least 10']
}
}
)
nongos.start()Done! Now you have a fully RESTful resource out of the box (CRUD). Give it a try:
GET http://localhost:1337/users
GET http://localhost:1337/users/:id
POST http://localhost:1337/users
PUT http://localhost:1337/users/:id
DELETE http://localhost:1337/users/:idConfiguration
Default configuration
{
env: process.env.NODE_ENV ? process.env.NODE_ENV.trim() : 'development',
port: process.env.PORT || 1337,
db: {
uri: 'mongodb://localhost',
options: {
useMongoClient: true
}
},
logger: {
transports: [
new (winston.transports.Console)({
colorize: true,
timestamp: true
})
]
}
}For more information on how to configure the path db.options, check the mongoose documentation.
For more information on how to configure the path logger, check the winston repository.
Changing the configuration
const nongos = require('nongos')
nongos.config({
port: process.env.PORT || 3000,
db: {
uri: 'mongodb://myuser:[email protected]:59767/temp'
}
})
// ...Bootstrapping
If you want to override the default bootstrap you can use nongos.bootstrap(), just be aware that by doing so, the default bootstrap will no longer execute.
const nongos = require('nongos')
nongos.resource(
'users',
{
name: String,
age: Number
}
)
nongos.bootstrap(() => {
nongos.app.use(require('./my-middleware'))
})
nongos.start()Resources
Validation
Because nongos uses mongoose you are able to validate your schema using the mongoose built-in rules or you can also create your own.
Check the full documentation at the mongoose website.
Methods
You can override or disable the default resource methods and also create new methods:
const nongos = require('nongos')
const {router, model} = nongos.resource(
'users',
{
name: String,
age: Number
},
{
update: false,
delete: false
}
)
router.put('/:id', (req, res) => res.status(400).send({message: 'Nope!'}))
router.get('/foo', (req, res) => res.send('bar'))
router.post('/john-doe', (req, res, next) => {
model.create({name: 'John Doe', age: 50}, (err, result) => {
if (err) return next(err)
res.send(result)
})
})
nongos.start()After these changes you will get:
GET http://localhost:1337/users // 200
GET http://localhost:1337/users/foo // 200 "bar"
GET http://localhost:1337/users/:id // 200
POST http://localhost:1337/users // 200
POST http://localhost:1337/users/john-doe // 200 {"__v": 0, "name": "John Doe", "age": 50, "_id": "5a3320b4e99a953ff07729a3"}
PUT http://localhost:1337/users/:id // 400 {"message": "Nope!"}
DELETE http://localhost:1337/users/:id // 404Querying
You can use some advanced querying thanks to querymen.
For more information check its documentation.
GET http://localhost:1337/posts?page=2&limit=30
GET http://localhost:1337/posts?user=foobar&fields=title,content
GET http://localhost:1337/posts?limit=10&sort=-createdAtAdditional options
As seen above in the validation block, you can use the optional third parameter of nongos.resource() to override the default CRUD behavior.
In addition to that you can also configure the keywords feature powered by mongoose-keywords. Check its documentation for more information on how to do so.
const nongos = require('nongos')
const {router, model} = nongos.resource(
'users',
{
name: String,
age: Number
},
{
list: true,
create: true,
read: true,
update: false,
delete: false,
keywords: {
path: ['name']
}
}
)
nongos.start()Stack explained
- mongoose: MongoDB object modeling tool
- winston: logger
- express: web framework / routing
- body-parser: body parsing middleware
- morgan: HTTP request logger middleware
- compression: compression middleware
- cors: CORS middleware
- helmet: security good practices for
express - querymen: querystring parser middleware for MongoDB
- mongoose-keywords:
mongooseplugin for searching
License
MIT
