1.1.5 • Published 5 years ago

node-express-mongodb v1.1.5

Weekly downloads
13
License
MIT
Repository
github
Last release
5 years ago

NODE-EXPRESS-MONGODB.API

This project allows you to create a powerful REST API in a very short time. It relies on

NodeJS | Express | MongoDB | Mongoose | PassportJS

npm npm npm

GitHub forks GitHub stars

Table of Contents

Instalation

npm install node-express-mongodb --save

Usage

Once express application is created we need to instantiate node-express-mongodb:

require('node-express-mongodb')(app, options, passport);

Options

KeyDescription
APP_NAMEName of the application
MONGODB_URIMongoDB database URI
HOSTHost url. Only for Postman purposes (optional)
PORTPort number. Only for Postman purposes (optional)
MODELS_PATHPath where the models are located. Default: ./app/models (optional)
DATA_PATHPath where data for generation is located. Default: ./app/data (optional)

Models

The way it generates the API is by reading each of the model definitions at models path. We need to create one definition for each of the models in our app. This needs to be a json similar to the one below:

{
    "name": "BasicExample",
    "route": "basicexample",
    "properties": {
        "property1": {
            "type": "String"
        },
        "property2": {
            "type": "String",
            "methodsNotAllowed": {
                "update": true
            }
        },
        "property3": {
            "type": "Number",
            "methodsNotAllowed": {
                "add": true
            }
        }
    },
    "methods": {
        "getAll": { "enabled": true },
        "get": { "enabled": true, "passportStrategy": "admin" },
        "add": { "enabled": true },
        "update": { "enabled": true },
        "remove": { "enabled": true }
    }
}

Once the app is started it generates all the routes based on them.

Default routes

There are 5 default routes. These are created if they are activated in the model.

RouteDescription
GET api/modelRouteReturns all the documents
GET api/modelRoute/:idReturns one document
POST api/modelRouteCreates one document
PUT api/modelRoute/:idUpdates one document
DELETE api/modelRoute/:idRemove one document

Custom routes

It is possible to create custom routes. For this you have to add a route file to ./app/routes and the service to use to ./app/services. Both files must be named as the route in modeldefinition.

Authentication

It is possible to add authentication to the routes by PassportJS. The way to do this is by defining the passportStrategyin the method object:

"methods": {"get": { "enabled": true, "passportStrategy": "admin" }}

! Note that in this case admin strategy would need to be defined in passport.js

Data generation

It is possible to auto generate data in the DB for dev purposes by just adding a collection to a json file named as the model in ./generation/data.

In order to run this task an extra endpoint is set up. /api/admin/generate.

Postman project generation

A Postman configuration object is generated by default so it can be easily imporated in Postman to get the collection of all endpoints in the app.

All you have to do in Postman is

Import > Import From Link and copying the follow app endpoint:

/api/postman

License

MIT

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

6 years ago

1.0.23

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago