Rapid NodeJS REST API Server generator with generator-restgoose and yo
Rapid NodeJS REST API Server generator with generator-restgoose and yo
So I just finished writing and testing the all new generator-restgoose, inspired (forked) from Antonio. I basically changed the core pattern of how MVC code is generated and segregated them, optimized and upgraded many deprecated code, and updated the mocha test. Here is the Rapid NodeJS REST Server generator I have been talking about.
Minimum Node Version : 4.x
Minimum NPM Version : 3.x
Quick Links
If you would like to contribute, please refer to guidelines and a list of open tasks.
For more information, please refer to the Wiki page and FAQ
Overview
- Scaffolds out-of-the-box REST API Server
- Generates All Ready to run files
- Model ( Mongoose)
- Api Route (endpoint)
- Api Object (Business Layer, crud logic)
- Documentation (API reffernce)
- Test File
- Generates ready-to-rock README file with License(MIT), etc.
- Conforms to jSend Specs for API JSON Response
- Modular and based on
Seperation of Concern
- Fast and readible code generation
- … much more
Table of contents
- Generator Restgoose
- Quick Links
- Overview
- Table of Contents
- Getting Started
- Command List
- Architecture
- ChangeLog
- Bucket List
- Guidelines for Contribution
- Generator Restgoose Core Team
- License
Getting Started
What is Yeoman?
Trick question. It’s not a thing. It’s this guy:
Basically, he wears a top hat, lives in your computer, and waits for you to tell him what kind of application you wish to create.
Not every new computer comes with a Yeoman pre-installed. He lives in the npm package repository. You only have to ask for him once, then he packs up and moves into your hard drive. Make sure you clean up, he likes new and shiny things.
1 | $ npm install -g yo |
Generator Restgoose
While running through a leafy mongodb field he picked up mongoose.
Generator-Restgoose scaffolds all API Routes, API Objects ( Bussiness Logic), Schema Models, Test Cases, and even Documentation for you with full CRUD functionality.
To install generator-restgoose from npm, run:
1 | $ npm install -g generator-restgoose |
Finally, initiate the generator:
1 | $ yo restgoose |
It should output a file structure similiar to:
1 | .bowerrc |
- models - contains Mongoose Schema of an entity ( Data Layer)
- apiObjects - Contains business logic & model access for each entity ( Business Layer )
- api - Contains routes of each entity ( Presentation Layer / Controller )
- test - contains unit test cases for each entity
- docs - contains Detailed
markdown
formatted documentation of each Schema generated through the sub-generator.
Don’t forget to checkout the config/lib.js
file which contains many useful stuff (Json Validator, image to Base64 Converter, etc).
Use the Library as var l = require('../config/lib.js);
( which generates with every sub schema).
Try to use l.p('Print something');
to log something. This will log to debug.log file.
Logging
Two log files are served : debug.log and access.log.
All responses are in valid jSend spec conformed JSON format.
Run the app
Development mode
1 | $ grunt |
or
1 | $ grunt server |
Production mode
1 | $ grunt prod |
Sub Generators
Schema
Creates all required Model, Controllers, test cases, routes and documentations for a particular Schema. Run the sub generator for schemas:
1 | $ yo restgoose:schema "article|title:String,excerpt:String,content:String,published:Boolean,created:Date" |
output:
1 | You're creating a schema for article |
DeleteSchema
Deletes all files (Model, APi Route, API Object, Doc, Test) for a particular schema. Run the sub generator for deleteschema:
1 | $ yo restgoose:deleteschema "article" --force |
N.B. You need to use –force else y=Yeoman will continue asking you to overwrite each file.
output:
1 | You're deleting all files for schema: article |
Getting To Know Yeoman
Yeoman has a heart of gold. He’s a person with feelings and opinions, but he’s very easy to work with. If you think he’s too opinionated, he can be easily convinced.
If you’d like to get to know Yeoman better and meet some of his friends, Grunt and Bower, check out the complete Getting Started Guide.
Command List
- yo restgoose
- initiate a project - yo restgoose:schema “
|field:DataType” - Create all routers, models, test and docs for a schema - yo restgoose:deleteschema “
“ - Delete all related generated files for a schema - grunt - start server with watch enabled
- npm test - Start Testing generated schemas
Architecture
WIP
ChangeLog
[ 28 Jan 2017 ] v0.1.8
- Features
- Universal Search Functionality ( both Strict and Casual Options)
- Optimizations & Upgrades
- Updated Docs
- Bug Fixes
- fixed test mothod in api
[ 8 Jan 2017 ] v0.1.6
First off, I am really sorry for this long awaited update. Good News, a lot has changed in this update - Unified Response JSON object, conforming with spec, Documentation Generation, Great README file for both Yeoman Generator-Restgoose Project(this one) and your generated project! Oh and by the way, do check out the brand new sub-generator “deleteschema”!
- Migration
With as much pain I can endure, this version of generator-restgoose is not backwards-compatible. This comes due to changing the core response object of all API endpoints. I will write up a blog page/wiki very soon for reasons, resolutions and troubleshooting. I personally regret inconveniences caused.
Despite this, If you need more reasons to keep using this project, read on … - Features
- Unified JSON response object (jSend)
- Quick Auto Callback and Response Handlers ( check
config/lib.js
) - Sub-Generator generates document for your schema
- Wiki & FAQs
- generated project README
- New Sub-Generator for deleting a schema, and its files.
- Optimizations & Upgrades
- generator-restgoose README (this one)
- Dedicated github-page and website
- Changed all broken links
- Updated All Dependencies
- Better & Clean Code Generation
- Better Test Cases
[ 5 Feb 2016 ] v0.1.5
- Bug Fixed for Test Route in APIs
Bucket List
Complete ReadME file for the generated api projectPre-defined generated docs for the generated API endpointsComplete README file for generator-restgoose project (this one)- API Versioning System
- API Dynamic on-demand Doc Generator
- Conform to REST API Standards
- out-of-the-box passportJS authentication with JWT
- Socket.IO Generator (wohooo)
- Docker Scripts
- AutoStart Script using PM2 ( 1 click deploy)
- Write Reasons, Troubleshooting and migration for v0.1.5 to 0.1.6 in a wiki/blog
Generator Restgoose Core Team
- Development & Maintenance : Abhishek Deb
License
MIT
Copyright 2017 Abhishek Deb
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
:warning: THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.