Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aeberdinelli/node-api
A working NodeJS API with dynamic endpoints to quickly start your project.
https://github.com/aeberdinelli/node-api
api expressjs mongodb node
Last synced: 6 days ago
JSON representation
A working NodeJS API with dynamic endpoints to quickly start your project.
- Host: GitHub
- URL: https://github.com/aeberdinelli/node-api
- Owner: aeberdinelli
- Created: 2019-07-22T17:58:02.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-10T23:18:29.000Z (almost 2 years ago)
- Last Synced: 2024-05-21T02:18:09.132Z (6 months ago)
- Topics: api, expressjs, mongodb, node
- Language: JavaScript
- Homepage:
- Size: 201 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Node-api
Quickly start your project by just cloning this repo!## Install
Just clone this repo, run `npm install` and you should have a complete node crud api. Of course, you need NodeJS and MongoDB installed first. Here's a detailed step by step:- [Install NodeJS](https://nodejs.org/es/) version >= 10
- [Install MongoDB](https://www.mongodb.com)
- Clone this repo: `git clone https://github.com/aeberdinelli/node-api.git` (you can also fork this so you can just push to your own repo with this API as a base)
- Install node dependencies: `npm install`
- Set log outputs:
- On Mac/Linux: `export DEBUG=API*`
- On Windows: `set DEBUG=API*`
- Run! `npm start`## Using
This api will read all the mongoose schemas inside `src/schemas` and create endpoints for each one of them. There's already a schema that you can use as an example which is also needed in order for this API to work.For example, if you want to create a CRUD for books, you can create a schema file like this:
```javascript
const mongoose = require('mongoose');let SchemaBooks = new mongoose.Schema({
'title': {
type: String
},
'author': {
type: String
},
'deleted': {
type: Boolean,
default: null
}
});module.exports = mongoose.model('Book', SchemaBooks);
```## Permissions
This API supports permissions per user per endpoint. For example, if you want to add a user that can create, delete and read books but can only read authors, you should add a document like this in the users collections in MongoDB:```javascript
{
"name" : "An user who have full access for the book endpoint but read only for author",
"lastname" : "Some",
"nickname" : "User",
"email" : "some@user",
"phone" : "+1 321 1234567",
"password" : "$2b$10$RqMmS35qslNgqFwebcwy4.g3gfVic51u3bAeAtytAPcpjHmQth/bm",
"privileges" : [
{
"model" : "book",
"methods" : [
"GET",
"POST",
"PUT",
"DELETE"
]
},
{
"model" : "author",
"methods" : [
"GET"
]
}
],
"deleted" : null
}
```**Tip**: You can create a POST request to the `user` endpoint to create it and update the privileges later so the API will encrypt the password for you. (And for security reasons, the API will ignore the privileges property on the request).
## Settings
To make this API easily configurable in different environments, most of the settings are used from environment variables. Here's a table with the available vars.
Name
Type
Default value
Doc
PORT
number
3000
Defines the port to use in the API
PRETTY_PRINT
boolean
false
(Works only if you have views), sets if pretty prints the HTML when you use a template engine
MONGODB_URL
string
mongodb://localhost:27017/
The URL for the MongoDB connection
MONGODB
string
null
required
Name of the database to use
DEBUG
string
null
This sets the logs output to the console. I recommend you use this value:API*
. That will enable all logs for the API and its sublevels
JWT_SIGNATURE
string
null
required
The JWT signature. You can use any word just likepepe
. Also, you can change this to disable all the existing JWT tokens forcing a new login
JWT_LIFETIME
string
9
The JWT session token lifetime, in hours.
GUEST_PRIVILEGES
array
['GET']
An array with the allowed http verbs for a guest (for example, if you want all guest to be able to create and read things, you should set this to['POST','GET']
)