Skip to content

marlonfurtado/api-nodejs

Repository files navigation

Template - API Node.js

RESTful API template with NodeJS, Express and Mongoose to personal use

See the API's documentation.

Stack

  • Node.js
  • Express
  • Mongoose

Directory structure

Overview

src/
├─ api/
│  ├─ auth/
│  ├─ user/
│  │  ├─ controller.js
│  │  ├─ index.js
│  │  ├─ model.js
│  └─ index.js
├─ services/
│  ├─ express/
│  ├─ google/
│  ├─ jwt/
│  ├─ mongoose/
│  ├─ passport/
│  └─ response/
├─ app.js
├─ config.js
└─ index.js

Commands

npm run dev # run the API in development mode
npm run prod # run the API in production mode
npm run docs # generate API docs
npm run lint # lint using ESLint

Run local example

We assume you have pre-installed the stack.

Run the server in development mode.

$ npm run dev
Express server listening on http://0.0.0.0:9000, in development mode

Note that creating and authenticating users needs a master key (which is defined in the .env file. Talk with admin!)

I. Sign In:

curl -X POST http://0.0.0.0:9000/api/auth -i -u [email protected]:qwerty -d "access_token=MASTER_KEY_HERE"

II. Create a user (sign up):

curl -X POST http://0.0.0.0:9000/api/users -i -d "name=test&[email protected]&password=qwerty&role=admin&access_token=MASTER_KEY_HERE"

Sign in (I) or sign up (II), it will return something like:

HTTP/1.1 201 Created
...
{
  "token": "GENERATED_TOKEN",
  "user": {
    "id": "58c2a275b80dff716c8a2efa",
    "name": "test",
    "email": "[email protected]",
    "createdAt":"2017-12-12T22:00:00.666Z"
  }
}

Now you can use the 'GENERATED_TOKEN' to call user protected APIs and get a user.

curl -X GET http://0.0.0.0:9000/api/users?access_token=GENERATED_TOKEN

Use npm run lint! :shipit:

A RESTful API generated by generator-rest and adapted by Marlon Furtado.
For more informations see the docs.