alpersonalwebsite / node-express-mongo-db

Node, Express and MongoDB
MIT License
0 stars 1 forks source link
express mongodb mongoose nodejs restapi

Node, Express and MongoDB

js-standard-style License: MIT

Overview

This is an easy, basic and raw example of HOW to implement an API with Node, Express and MongoDB/Atlas (with Mongoose ODM).

Requirements

Install dependencies

npm install

Running the server

Development

npm run dev

Production

npm run build

npm start

API endpoints

GET /api/users

Request:

curl http://127.0.0.1:3333/api/users

Sample response:

{
  "data": [
    {
      "_id": "60da6b74fc13ae7069000d4b",
      "firstname": "Dedra",
      "lastname": "Demangel",
      "age": 96,
      "gender": "Agender",
      "username": "ddemangel3",
      "company": "Skidoo",
      "email": "ddemangel3@networkadvertising.org",
      "phone": "759-142-2883",
      "address": "4 Fisk Drive"
    },
    {
      "_id": "60da6b74fc13ae7069000d48",
      "firstname": "Martynne",
      "lastname": "Pimmocke",
      "age": 79,
      "gender": "Female",
      "username": "mpimmocke0",
      "company": "Edgeify",
      "email": "mpimmocke0@amazonaws.com",
      "phone": "354-508-5487",
      "address": "20633 Stone Corner Trail"
    }
  ]
}

Query string

GET /api/users?limit=1
Request:
curl http://127.0.0.1:3333/api/users?limit=1
Response:
{
  "data": [
    {
      "_id": "60da6b74fc13ae7069000d4b",
      "firstname": "Dedra",
      "lastname": "Demangel",
      "age": 96,
      "gender": "Agender",
      "username": "ddemangel3",
      "company": "Skidoo",
      "email": "ddemangel3@networkadvertising.org",
      "phone": "759-142-2883",
      "address": "4 Fisk Drive"
    }
  ]
}

Wrong type for n value will return all the users. Example: users?limit=%27Hello%27

GET /api/users?offset=10
Request:
curl http://127.0.0.1:3333/api/users?offset=10
Response:
{
  "data": [
    {
      "_id": "60da6b74fc13ae7069000d55",
      "firstname": "Cindi",
      "lastname": "Harnell",
      "age": 57,
      "gender": "Bigender",
      "username": "charnelld",
      "company": "Skyble",
      "email": "charnelld@networkadvertising.org",
      "phone": "176-961-8815",
      "address": "09953 Susan Alley"
    },
    {
      "_id": "60da6b74fc13ae7069000d57",
      "firstname": "Olva",
      "lastname": "Hoodless",
      "age": 61,
      "gender": "Female",
      "username": "ohoodlessf",
      "company": "Jaloo",
      "email": "ohoodlessf@illinois.edu",
      "phone": "886-598-0586",
      "address": "8 Chinook Pass"
    }
  ]
}

GET /latency

Request:

curl http://127.0.0.1:3333/latency

Response:

{
  "data": "Thanks for waiting 1 second"
}

Query string

GET /latency?delay=2000

Wrong type for n value will produce a default delay of 1000ms.

Request:
curl http://127.0.0.1:3333/latency?delay=2000
Response:
{
  "data": "Thanks for waiting 2 seconds"
}

GET everything else

Request:

curl http://127.0.0.1:3333/

Response:

{
  "message": "Node.js, Express, and MongoDB API!"
}

Notes:

If you need help at the tie of Migrating from mLab to MongoDB Atlas