GithubHelp home page GithubHelp logo

fake-api's Introduction

Fake API NodeJS

Get a full fake API as soon as possible. Base on NodeJS + JSON Server + Socket.IO.

Features:

  • Define your own database using a json file, generate REST APIs from that database.

  • Register user with username & password or email & password. Using object users in the database.

  • Login with registered users.

  • Protect resources using JWT Bearer authentication.

  • Upload files.

  • Send and receive messages over web socket connection (Socket.IO).

Preview: https://nodejs-fake-api.herokuapp.com

Getting started

1. Clone this repository

git clone https://github.com/robinhuy/fake-api-nodejs.git

or fork to your account and clone the forked repo

2. Install dependencies

cd fake-api-nodejs
npm install

or if you using yarn

cd fake-api-nodejs
yarn install

3. Run server

  • Production mode:

    npm start

    or

    yarn start
  • Development mode (auto reload server when editing using nodemon):

    npm run dev

    or

    yarn dev
  • The server will run on http://localhost:8000. You can test with public endpoint: http://localhost:8000/products (GET method).

Modify your data

All the data was placed in database.json. Edit it to suit your purpose but keep object users to use authentication feature.

You can use https://mockaroo.com/ to mock data, and publish your code to https://heroku.com/ or similar hosting to get a Public API.

Note:

  • To protect resources, decleare resources and protected methods in database.json:

    "protectedResources": {
      "users": ["GET", "POST", "PUT", "PATCH", "DELETE"],
      "products": ["POST", "PUT", "PATCH", "DELETE"]
    }
  • To register new user, using endpoint /register, method POST, request type application/json. Body request like users resources:

  • To login, using endpoint /login, method POST, request type application/json. Body request like this:

    {
      "username": "admin",
      "password": "admin"
    }

    or

    {
      "email": "[email protected]",
      "password": "admin"
    }
  • To renew AccessToken, using endpoint /renew-token, method POST, request type application/json. Body request like this:

    {
      "refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTY1NjMyNzE4NiwiZXhwIjoxNjU2MzI4MDg2fQ.-si1n7yHpjQ2LEyYqZT6ClIFJOqLOeVXRhwjzyvEZMo"
    }
  • To upload single file, using endpoint /upload-file, method POST, request type form-data, field file. Uploaded file stored in /public/uploads/.

  • To upload multiple files, using endpoint /upload-files, method POST, request type form-data, field files. Uploaded files stored in /public/uploads/.

  • Change default port, database file, jwt secret or jwt token expires in config.js.

Access & modify API

Please view detailed document in https://github.com/typicode/json-server/blob/master/README.md#table-of-contents

If you want to change logic of authentication or add more custom REST endpoints, please edit file server.js and src/rest.js.

Edit src/socket-io.js to add or modify Socket.IO events, src/graphql.js to add or modify GraphQL query/mutation (currently, authentication aren't being applied to Socket.IO & GraphQl endpoints).

Default Endpoints

View and modify resources in database.json.

Open Endpoints

Open endpoints do not require Authentication.

User

  • Login: POST /login

  • Register: POST /register

Product

  • Get products: GET /products

  • Get product by ID: GET /products/:id

Media

  • Upload single file: POST /upload-file

  • Upload multiple files: POST /upload-files

Private Endpoints (require Authentication)

Private endpoints require a valid Token to be included in the header of the request. A Token can be acquired from the Login view above.

User

  • Get users: GET /users

  • Get user by ID: GET /users/:id

  • Create user: POST /users

  • Update user (entire information): PUT /users/:id

  • Update user (partial information) PATCH /users/:id

Product

  • Create product: POST /products

  • Update product (entire information): PUT /products/:id

  • Update product (partial information) PATCH /products/:id

Web Socket (Socket.IO)

  • Event emit: Echo message to sender

    socket.emit("emit", "Hello");
  • Event broadcast: Broadcast message to all clients in the current namespace except the sender

    socket.emit("broadcast", "Hello");
  • Event broadcast-all: Broadcast message to all clients in the current namespace include the sender

    socket.emit("broadcast-all", "Hello");
  • Event join-room: Join a room

    socket.emit("join-room", "game");
  • Event emit-in-room: Send message to all clients in the room except the sender

    socket.emit("join-room", { room: "game", event: "chat", msg: "Hello" });

GraphQL

  • Endpoint: /graphql.

  • Get objects by name (objects declared in database.json)

    query getData($objectName: String!) {
      getObjects(objectName: $objectName)
    }

    Query variables:

    {
      "objectName": "products"
    }
  • Get an object by name, search by property

    query getData($objectName: String!, $objectKey: String!, $objectValue: ObjectValue) {
      getObjectByKey(objectName: $objectName, objectKey: $objectKey, objectValue: $objectValue)
    }

    ObjectValue must specify the data type:

    ObjectValue {
      int: Int
      float: Float
      string: String
      boolean: Boolean
    }

    Query variable examples:

    {
      "objectName": "products",
      "objectKey": "id",
      "objectValue": {
        "int": 1
      }
    }
    {
      "objectName": "products",
      "objectKey": "name",
      "objectValue": {
        "string": "Grapes - Black"
      }
    }
  • Create an object

    query CreateObject($objectName: String!, $objectData: JSONScalarType!) {
      createObject(objectName: $objectName, objectData: $objectData)
    }

    Query variable examples:

    {
      "objectName": "posts",
      "objectData": {
        "title": "New post"
      }
    }
  • Update an object

    query UpdateObject($objectName: String!, $objectId: ID!, $objectData: JSONScalarType!) {
      updateObject(objectName: $objectName, objectId: $objectId, objectData: $objectData)
    }

    Query variable examples:

    {
      "objectName": "posts",
      "objectId": "1",
      "objectData": {
        "title": "Update post"
      }
    }
  • Delete an object

    query DeleteObject($objectName: String!, $objectId: ID!) {
      deleteObject(objectName: $objectName, objectId: $objectId)
    }

    Query variable examples:

    {
      "objectName": "posts",
      "objectId": "1"
    }

    ...

fake-api's People

Contributors

mothuc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.