UNPKG

4.58 kBMarkdownView Raw
1# NODE-EXPRESS-MONGODB.API
2
3This project allows you to create a powerful REST API in a very short time. It relies on
4
5[NodeJS](https://nodejs.org/en/) | [Express](https://expressjs.com/)
6| [MongoDB](https://www.mongodb.com/) | [Mongoose](http://mongoosejs.com/) | [PassportJS](http://www.passportjs.org/)
7
8<!-- Badges section here. -->
9
10[![npm](https://img.shields.io/npm/v/node-express-mongodb.svg)][npm-badge-url]
11[![npm](https://img.shields.io/npm/l/node-express-mongodb.svg)][npm-badge-url]
12[![npm](https://david-dm.org/pabloibanezcom/node-express-mongodb/status.svg)][david-dependency-url]
13
14[![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)
15[![GitHub stars](https://img.shields.io/github/stars/pabloibanezcom/node-express-mongodb.svg?style=social&label=Star)](https://github.com/pabloibanezcom/node-express-mongodb)
16
17## Table of Contents
18
19* [Instalation](#instalation)
20* [Usage](#usage)
21* [Authentication](#authentication)
22* [Data generation](#data-generation)
23* [Postman project generation](#postman-project-generation)
24* [License](#license)
25
26## Instalation
27
28```bash
29npm install node-express-mongodb --save
30```
31
32## Usage
33
34Once express application is created we need to instantiate node-express-mongodb:
35
36```bash
37require('node-express-mongodb')(app, options, passport);
38```
39
40### Options
41
42Key | Description
43--- | ---
44APP_NAME | Name of the application
45MONGODB_URI | MongoDB database URI
46HOST | Host url. *Only for Postman purposes (optional)*
47PORT | Port number. *Only for Postman purposes (optional)*
48MODELS_PATH | Path where the models are located. *Default: `./app/models`* *(optional)*
49DATA_PATH | Path where data for generation is located. *Default: `./app/data`* *(optional)*
50
51### Models
52
53The 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:
54
55```bash
56{
57 "name": "BasicExample",
58 "route": "basicexample",
59 "properties": {
60 "property1": {
61 "type": "String"
62 },
63 "property2": {
64 "type": "String",
65 "methodsNotAllowed": {
66 "update": true
67 }
68 },
69 "property3": {
70 "type": "Number",
71 "methodsNotAllowed": {
72 "add": true
73 }
74 }
75 },
76 "methods": {
77 "getAll": { "enabled": true },
78 "get": { "enabled": true, "passportStrategy": "admin" },
79 "add": { "enabled": true },
80 "update": { "enabled": true },
81 "remove": { "enabled": true }
82 }
83}
84```
85
86Once the app is started it generates all the routes based on them.
87
88### Default routes
89
90There are 5 default routes. These are created if they are activated in the model.
91
92Route | Description
93--- | ---
94GET api/[modelRoute] | Returns all the documents
95GET api/[modelRoute]/:id | Returns one document
96POST api/[modelRoute] | Creates one document
97PUT api/[modelRoute]/:id | Updates one document
98DELETE api/[modelRoute]/:id | Remove one document
99
100### Custom routes
101
102It 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.
103
104## Authentication
105
106It is possible to add authentication to the routes by PassportJS. The way to do this is by defining the `passportStrategy`in the method object:
107
108`"methods": {"get": { "enabled": true, "passportStrategy": "admin" }}`
109
110**[!]** Note that in this case admin strategy would need to be defined in passport.js
111
112## Data generation
113
114It 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`.
115
116In order to run this task an extra endpoint is set up. ***/api/admin/generate***.
117
118## Postman project generation
119
120A 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.
121
122All you have to do in Postman is
123
124*Import > Import From Link* and copying the follow app endpoint:
125
126***/api/postman***
127
128## License
129
130MIT
131
132
133[npm-badge-url]: https://www.npmjs.com/package/node-express-mongodb
134[david-dependency-url]: https://david-dm.org/pabloibanezcom/node-express-mongodb
135