Yeoman generator for creating RESTful NodeJS APIs, using ES6, Mongoose and Express. The fastest way to get your project up and running using an awesome stack.
- Make sure you have yeoman installed on your machine:
npm install -g yo
- Install the generator globally:
npm install -g generator-api
yo api, or
Running the generated project
Make sure you have node version
>= 6 because this project uses native supported ES6 features.
mongodto start the local mongodb in a separated terminal instance (If you don't have mongodb installed locally, visit It's webpage to learn how to install it).
npm run devto run the app (By default the app will run at
localhost:8080, you can change this in the config file).
Did you choose Docker (🐳) support?
docker-compose upto run the app. You might need
sudofor this one.
NOTE: The Dockerfile uses
node:latest as its starting point, if you wish to use another version of Node check out the available ones here.
You'll likely be consuming mongodb as a service, so make sure to set the env var pointing at it. Then run
Build the Docker container and run it:
sudo docker build -t <image-name> .sudo docker run \-p <host-port>:8080 \-d <image-name> \-e MONGO_DB_URI=mongodb://<username>:<password>@<host>:<port> \npm run start
Assuming we use
pet models the generated project will look like this:
├───index.js ├───routes.js ├───package.json ├───config.js └───lib/ | ├───controller.js | ├───facade.js └───model/ ├───user/ │ └───controller.js | └───facade.js | └───router.js | └───schema.js └───pet/ └───controller.js └───facade.js └───router.js └───schema.js
HTTP layer, in this instance you can manage express request, response and next. In
lib/controller are the basic RESTful methods
remove. Because this class is extending from there, you got that solved. You can overwrite extended methods or create custom ones here.
This layer works as a simplified interface of mongoose and as business model layer, in this instance you can manage your business logic. Here are some use case examples:
- Validate collection
xbefore creating collection
- Create collection
xbefore creating collection
lib/facade you have the basic support for RESTful methods. Because this class is extending from there, you got that solved. You can overwrite extended methods or create custom ones here. Also you can support more mongoose functionality like
Once you have the generated project, if you want to add a new model you can simply run
yo api:model. This will generate a new folder under
model, in order to make it work, you just need to import the route into the
yo api:model, write foo (What ever model name you want)
- Go to
routes.jsand import the new generated model route
const foo = require('./model/foo/router')
- Use the imported route
Contributors are welcome, please fork and send pull requests! If you find a bug or have any ideas on how to improve this project please submit an issue.