项目作者: HugoLiconV

项目描述 :
Version 2 API con autentificación
高级语言: JavaScript
项目地址: git://github.com/HugoLiconV/shop-scraper-api-v2.git
创建时间: 2019-04-13T04:01:55Z
项目社区:https://github.com/HugoLiconV/shop-scraper-api-v2

开源协议:

下载


shop-scraper

A RESTful API generated by generator-rest.

See the API’s documentation.

Commands

After you generate your project, these commands are available in package.json.

  1. npm test # test using Jest
  2. npm run coverage # test and open the coverage report in the browser
  3. npm run lint # lint using ESLint
  4. npm run dev # run the API in development mode
  5. npm run prod # run the API in production mode
  6. npm run docs # generate API docs

Playing locally

First, you will need to install and run MongoDB in another terminal instance.

  1. $ mongod

Then, run the server in development mode.

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

If you choose to generate the authentication API, you can start to play with it.

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

Create a user (sign up):

  1. curl -X POST http://0.0.0.0:9000/users -i -d "email=test@example.com&password=123456&access_token=MASTER_KEY_HERE"

It will return something like:

  1. HTTP/1.1 201 Created
  2. ...
  3. {
  4. "id": "57d8160eabfa186c7887a8d3",
  5. "name": "test",
  6. "picture":"https://gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?d=identicon",
  7. "email": "test@example.com",
  8. "createdAt": "2016-09-13T15:06:54.633Z"
  9. }

Authenticate the user (sign in):

  1. curl -X POST http://0.0.0.0:9000/auth -i -u test@example.com:123456 -d "access_token=MASTER_KEY_HERE"

It will return something like:

  1. HTTP/1.1 201 Created
  2. ...
  3. {
  4. "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
  5. "user": {
  6. "id": "57d8160eabfa186c7887a8d3",
  7. "name": "test",
  8. "picture": "https://gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?d=identicon",
  9. "email": "test@example.com",
  10. "createdAt":"2016-09-13T15:06:54.633Z"
  11. }
  12. }

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.

  1. 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:

  1. HTTP/1.1 201 Created
  2. ...
  3. {
  4. "id": "57d819bfabfa186c7887a8d6",
  5. "title": "Awesome Article",
  6. "content": "Yeah Baby",
  7. "createdAt": "2016-09-13T15:22:39.846Z",
  8. "updatedAt":"2016-09-13T15:22:39.846Z"
  9. }

Some endpoints are only accessible by admin users. To create an admin user, just pass the role=admin along to other data when calling POST /users.

Deploy

Here is an example on how to deploy to Heroku using Heroku CLI:

  1. # start a new local git repository
  2. git init
  3. # create a new heroku app
  4. heroku apps:create my-new-app
  5. # add heroku remote reference to the local repository
  6. heroku git:remote --app my-new-app
  7. # add the MongoLab addon to the heroku app
  8. heroku addons:create mongolab
  9. # set the environment variables to the heroku app (see the .env file in root directory)
  10. heroku config:set MASTER_KEY=masterKey JWT_SECRET=jwtSecret
  11. # commit and push the files
  12. git add -A
  13. git commit -m "Initial commit"
  14. git push heroku master
  15. # open the deployed app in the browser
  16. heroku open

The second time you deploy, you just need to:

  1. git add -A
  2. git commit -m "Update code"
  3. git push heroku master

Directory structure

Overview

You can customize the src and api directories.

  1. src/
  2. ├─ api/
  3. ├─ user/
  4. ├─ controller.js
  5. ├─ index.js
  6. ├─ index.test.js
  7. ├─ model.js
  8. └─ model.test.js
  9. └─ index.js
  10. ├─ services/
  11. ├─ express/
  12. ├─ facebook/
  13. ├─ mongoose/
  14. ├─ passport/
  15. ├─ sendgrid/
  16. └─ your-service/
  17. ├─ app.js
  18. ├─ config.js
  19. └─ index.js

src/api/

Here is where the API endpoints are defined. Each API has its own folder.

src/api/some-endpoint/model.js

It defines the Mongoose schema and model for the API endpoint. Any changes to the data model should be done here.

src/api/some-endpoint/controller.js

This is the API controller file. It defines the main router middlewares which use the API model.

src/api/some-endpoint/index.js

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.

services/

Here you can put helpers, libraries and other types of modules which you want to use in your APIs.