Giter VIP home page Giter VIP logo

generator-api's Introduction

generator-api

CircleCI bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies bitHound Code npm

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.

generator

Getting started

  • Make sure you have yeoman installed on your machine: npm install -g yo
  • Install the generator globally: npm install -g generator-api
  • Run: yo api, or yo and choose Api option

Running the generated project

Development

The app will run at localhost:8080.

Make sure you have node version >= 6.3 because this project uses native supported ES6 features.

  • Run: mongod to start the local mongodb in a separated terminal instance.
  • Run: npm run dev to run the app.

NOTE: If you don't have mongodb installed locally, visit their webpage.

Did you choose Docker support? 🐳

You only need Docker and docker-compose installed, no mongodb, no node, no npm. 😎

  • Run: docker-compose up to run the app. You might need sudo for this one.

production

you'll likely be consuming mongodb as a service, so make sure you set the env var to connect to it.

Just run npm start.

Wait, you choose Docker right? 🐳

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

Architecture

The idea is to be able to scale having a simple architecture. Assuming we use user and pet as models the generated project would look like this:

├───index.js
├───routes.js
├───package.json
├───config.js
└───lib/
|   ├───controller.js
|   ├───facade.js
└───model/
    ├───user/
    │   └───user-controller.js
    |   └───user-facade.js
    |   └───user-router.js
    |   └───user-schema.js
    └───pet/
        └───pet-controller.js
        └───pet-facade.js
        └───pet-router.js
        └───pet-schema.js

Controller:

HTTP layer, in this instance you can manage express request, response and next. In lib/controller are the basic RESTful methods find, findOne, findById, create, update and remove. Because this class is extending from there, you got that solved. You can overwrite extended methods or create custom ones here.

Facade:

This layer works as a simplified interface of mongoose and as Business Model layer, in this instance you can manage your business logic. For example, if you want to create a pet before creating a user, because you'll end up adding that pet to the person, this is the place.

In 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 skip, sort etc.

To do

  • Create more generator templates to add new models once the project was initialized
  • Implement testing in the generated project

Contributing

Contributors are welcome, please fork and send pull requests! If you have any ideas on how to improve this project please submit an issue.

License

MIT License

generator-api's People

Contributors

gillchristian avatar ndelvalle avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.