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 -S
Basic usage
const nongos = nongos nongosstart
Done! Now you have a fully RESTful resource out of the box (CRUD). Give it a try:
GET http://localhost:1337/usersGET http://localhost:1337/users/:idPOST http://localhost:1337/usersPUT http://localhost:1337/users/:idDELETE http://localhost:1337/users/:id
Configuration
Default configuration
env: processenvNODE_ENV ? processenvNODE_ENV : 'development' port: processenvPORT || 1337 db: uri: 'mongodb://localhost' options: useMongoClient: true logger: transports: new winstontransportsConsole 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 = nongos // ...
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 = nongos nongos nongosstart
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 = const router model = nongos router router router nongosstart
After these changes you will get:
GET http://localhost:1337/users // 200GET http://localhost:1337/users/foo // 200 "bar"GET http://localhost:1337/users/:id // 200POST http://localhost:1337/users // 200POST 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 // 404
Querying
You can use some advanced querying thanks to querymen
.
For more information check its documentation.
GET http://localhost:1337/posts?page=2&limit=30GET http://localhost:1337/posts?user=foobar&fields=title,contentGET http://localhost:1337/posts?limit=10&sort=-createdAt
Additional 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 = const router model = nongos nongosstart
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:
mongoose
plugin for searching
License
MIT