Coder Social home page Coder Social logo

node-express-rest-api-example's Introduction

node-express-rest-api-example

A Basic Node.js/Express REST API implementation example.

Full Tutorial at https://developerhowto.com/2018/12/29/build-a-rest-api-with-node-js-and-express-js/

Prerequisites

For Windows

  • Python 2.7 (for microsoft build tools)
  • Install Microsoft build tools (to build sqlite using node-gyp)
  • Node-gyp (npm install --global node-gyp)

Usage

  • Run npm install to installl dependencies
  • Run npm run start to start the local server
  • Load http://localhost:8000 to test the endpoint. It will display a json result {"message":"Ok"}

API Endpoints

GET /api/users

Get a list of users

{
  "message": "success",
  "data": [
    {
      "id": 1,
      "name": "admin",
      "email": "[email protected]",
      "password": "a66abb5684c45962d887564f08346e8d"
    },
    {
      "id": 2,
      "name": "user",
      "email": "[email protected]",
      "password": "4da49c16db42ca04538d629ef0533fe8"
    }
  ]
}

GET /api/user/{id}

Get user information by user id

{
  "message": "success",
  "data": {
    "id": 1,
    "name": "admin",
    "email": "[email protected]",
    "password": "a66abb5684c45962d887564f08346e8d"
  }
}

POST /api/user/

To create a new user based on POST data (x-www-form-url-encoded)

  • name: User name
  • email: User email
  • password: User password

Postman example

PATCH /api/user/{id}

To update user data by id, based on POST data (x-www-form-url-encoded)

  • name: User name
  • email: User email
  • password: User password

You can send only one attribute to update, the rest of the info remains the same.

In this example, using CURL you can update the user email:

curl -X PATCH -d "[email protected]" http://localhost:8000/api/user/2

DELETE /api/user/{id}

To remove a user from the database by user id.

This example is using the curl command line

curl -X "DELETE" http://localhost:8000/api/user/2

The result is:

{"message":"deleted","rows":1}

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.