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

nem-generator

v1.0.1

Published

A Node.js project generator using Express and connected to MongoBD.

Downloads

4

Readme

Node.js - Express - MongoDB Generator

npm version github stars

A Node.js project generator using Express and connected to MongoBD.

Installation

npm install -g nem-generator

Features

  • [x] Authentication middleware (with JWT)
  • [x] Authorization middleware (role-based access control)
  • [x] Connection to MongoDB (using Mongoose)
  • [x] Example for User model (schema, service, controller, router)
  • [x] Config file prepared for 3 environments (dev, test, prod)

Documentation

How to use

Simply run

nem-gen
  

and you will be prompted with questions, allowing you to configure your project (for now, the project name is the only configurable parameter during project creation).

Project Structure

This project follows a Model - Service - Controller - Router structure. This is well explained in this StackOverflow answer followed to design this solution.
The generated project has the following files:

    .
    ├── app.js
    ├── bin
    │   └── www
    ├── config
    │   ├── config.js
    │   ├── config.json
    │   ├── mongod.conf
    │   └── roles.js
    ├── controllers
    │   └── userController.js
    ├── models
    │   └── user.js
    ├── package.json
    ├── routes
    │   ├── authorizeRole.js
    │   ├── usersRoute.js
    │   └── verifyToken.js
    └── services
        └── userService.js

Config File

In config/config.json you can configure global variables to use in your project.

| Parameter | Description | Default values | Required | |-----------|-------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------|----------| | config_id | Environment name | ["development","testing","production"] for the corresponding environment | false | | app_name | Project name | name inserted in project creation | false | | node_port | Port where the project will run | 5000 (if no value exist in the config) or [3000, 3000, 3001] for the corresponding environment | false | | database | URL to a MongoDB database | "mongodb://127.0.0.1/{project-name}-{environment}" | true | | secret | Secret for the JWT. Make sure you change this to secure your tokens | "supersecret" | true | | tokenLife | Duration of the token expressed in seconds or a string describing a time span zeit/ms | 2678400 (31 days) | true |

Make sure you always have the required fields at least in "development", since the default project structure uses these. You can only repeat config variables in other environments if you want to override the default config variable values found in the default "development" environment.

Generated default file:

{
    "development": {
        "config_id": "development",
        "app_name": "project-name",
        "node_port": 3000,
        "database": "mongodb://127.0.0.1/project-name-dev",
        "secret": "supersecret",
        "tokenLife": 2678400
    },
    "testing": {
        "config_id": "testing",
        "node_port": 3000,
        "database": "mongodb://127.0.0.1/project-name-test"
    },
    "production": {
        "config_id": "production",
        "node_port": 3001,
        "database": "mongodb://127.0.0.1/project-name-prod"
    }
}

You can add your own config parameters in this file, and use them in your code with global.gConfig.{parameter-name}, e.g. global.gConfig.database.

Authentication and Authorization

Make sure verifyToken middleware is always called before authorize, in order to extract the role of the received token.
Here is an example:

router.get('/', verifyToken, authorize(roles.ADMIN), async function (req, res){
    ...
}

This function is only authorized for tokens from users with 'Admin' role. You can also authorize multiple roles by passing an array to the authorize, p.e.

authorize([roles.USER, roles.ADMIN])

Remove restrictions

If you don't want any role restriction, just remove the authorize middleware.

You can also remove the token restriction, remove the verifyToken middleware.

TODO

  • [ ] Allow selection of database type (add support to SQL using Sequelize)
  • [ ] Allow roles configuration during project creation
  • [ ] Add optional client app template (for React, Angular or Vue)

Feel free to create issues with more ideas!

License

This project is licensed under the MIT License - see the LICENSE file for details