These commands are available in package.json
.
npm test # test using Jest
npm run coverage # test and open the coverage report in the browser
npm run lint # lint using ESLint
npm run dev # run the API in development mode
npm run prod # run the API in production mode
npm run docs # generate API docs
First, you will need to install and run MongoDB in another terminal instance and create a folder /data/db/ in the root drive (windows)
To run MongoDB:
$ mongod
Or on windows, run mongod.exe
2. After you've installed MongoDB, clone the repository of the project and install the dependencies.
$ git clone https://github.com/Angulz/showroom-back.git
$ cd showroom-back
$ npm install
$ npm run dev
Express server listening on http://0.0.0.0:9000, in development mode
Create a user (sign up):
curl -X POST http://0.0.0.0:9000/users -i -d "[email protected]&password=123456"
It will return something like:
HTTP/1.1 201 Created
...
{
"id": "57d8160eabfa186c7887a8d3",
"name": "test",
"picture":"https://gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?d=identicon",
"email": "[email protected]",
"createdAt": "2016-09-13T15:06:54.633Z"
}
Authenticate the user (sign in):
curl -X POST http://0.0.0.0:9000/auth -i -u [email protected]:123456 -d
It will return something like:
HTTP/1.1 201 Created
...
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
"user": {
"id": "57d8160eabfa186c7887a8d3",
"name": "test",
"picture": "https://gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?d=identicon",
"email": "[email protected]",
"createdAt":"2016-09-13T15:06:54.633Z"
}
}
Now you can use the eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
token (it's usually greater than this) to call user protected APIs. For example, you can create a new article
API using yo rest:api
and make the POST /articles
endpoint only accessible to authenticated users. Then, to create a new article you must pass the access_token
parameter.
curl -X POST http://0.0.0.0:9000/articles -i -d "title=Awesome Article&content=Yeah Baby&access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
It will return something like:
HTTP/1.1 201 Created
...
{
"id": "57d819bfabfa186c7887a8d6",
"title": "Awesome Article",
"content": "Yeah Baby",
"createdAt": "2016-09-13T15:22:39.846Z",
"updatedAt":"2016-09-13T15:22:39.846Z"
}
Some endpoints are only accessible by admin users. To create an admin user, just pass the
role=admin
along to other data when callingPOST /users
.
The src
and api
directories contain most of the code that can be modified.
src/
├─ api/
│ ├─ user/
│ │ ├─ controller.js
│ │ ├─ index.js
│ │ ├─ index.test.js
│ │ ├─ model.js
│ │ └─ model.test.js
│ └─ index.js
├─ services/
│ ├─ express/
│ ├─ facebook/
│ ├─ mongoose/
│ ├─ passport/
│ ├─ sendgrid/
│ └─ your-service/
├─ app.js
├─ config.js
└─ index.js
Here is where the API endpoints are defined. Each API has its own folder.
It defines the Mongoose schema and model for the API endpoint. Any changes to the data model should be done here.
This is the API controller file. It defines the main router middlewares which use the API model.
This is the entry file of the API. It defines the routes using, along other middlewares (like session, validation etc.), the middlewares defined in the some-endpoint.controller.js
file.
Here you can put helpers
, libraries
and other types of modules which you want to use in your APIs.