Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pabloibanezcom/node-express-mongodb

Skeleton api app with Node Express and MongoDB
https://github.com/pabloibanezcom/node-express-mongodb

api express mongodb mongoose nodejs passport-facebook passportjs postman

Last synced: about 16 hours ago
JSON representation

Skeleton api app with Node Express and MongoDB

Awesome Lists containing this project

README

        

# NODE-EXPRESS-MONGODB.API

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

[NodeJS](https://nodejs.org/en/) | [Express](https://expressjs.com/)
| [MongoDB](https://www.mongodb.com/) | [Mongoose](http://mongoosejs.com/) | [PassportJS](http://www.passportjs.org/)

[![npm](https://img.shields.io/npm/v/node-express-mongodb.svg)][npm-badge-url]
[![npm](https://img.shields.io/npm/l/node-express-mongodb.svg)][npm-badge-url]
[![npm](https://david-dm.org/pabloibanezcom/node-express-mongodb/status.svg)][david-dependency-url]

[![GitHub forks](https://img.shields.io/github/forks/pabloibanezcom/node-express-mongodb.svg?style=social&label=Fork)](https://github.com/pabloibanezcom/node-express-mongodb/fork)
[![GitHub stars](https://img.shields.io/github/stars/pabloibanezcom/node-express-mongodb.svg?style=social&label=Star)](https://github.com/pabloibanezcom/node-express-mongodb)

## Table of Contents

* [Instalation](#instalation)
* [Usage](#usage)
* [Authentication](#authentication)
* [Data generation](#data-generation)
* [Postman project generation](#postman-project-generation)
* [License](#license)

## Instalation

```bash
npm install node-express-mongodb --save
```

## Usage

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

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

### Options

Key | Description
--- | ---
APP_NAME | Name of the application
MONGODB_URI | MongoDB database URI
HOST | Host url. *Only for Postman purposes (optional)*
PORT | Port number. *Only for Postman purposes (optional)*
MODELS_PATH | Path where the models are located. *Default: `./app/models`* *(optional)*
DATA_PATH | Path 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:

```bash
{
"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.

Route | Description
--- | ---
GET api/[modelRoute] | Returns all the documents
GET api/[modelRoute]/:id | Returns one document
POST api/[modelRoute] | Creates one document
PUT api/[modelRoute]/:id | Updates one document
DELETE api/[modelRoute]/:id | Remove 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 `passportStrategy`in 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

[npm-badge-url]: https://www.npmjs.com/package/node-express-mongodb
[david-dependency-url]: https://david-dm.org/pabloibanezcom/node-express-mongodb