npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

generator-api

v1.5.3

Published

Yeoman generator for creating RESTful NodeJS APIs, using ES6, Mongoose and Express

Downloads

153

Readme

generator-api

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

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

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

Development

  • Run: mongod to 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).
  • Run: npm run dev to run the app (By default the app will run at localhost:8080, you can change this in the config file).

Did you choose Docker (:whale:) support?

You only need Docker and docker-compose installed, forget about having node, mongodb or npm.

  • Run: docker-compose up to run the app. You might need sudo for 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.

Production

You'll likely be consuming mongodb as a service, so make sure to set the env var pointing at it. Then run npm start.

Using Docker

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

Assuming we use user and 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

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. Here are some use case examples:

  • Validate collection x before creating collection y
  • Create collection x before creating collection y

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.

Model subgenerator

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 routes.js.

Example:

  • Run yo api:model, write foo (What ever model name you want)
  • Go to routes.js and import the new generated model route const foo = require('./model/foo/router')
  • Use the imported route router.use('/foo', foo)

Contributing

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.

License

MIT License