Skip to content

xbersoy/nodejs-mongo-rest-api

Repository files navigation

NodeJS Rest API with (ExpressJS and MongoDB)

NodeJS based rest service application

Installation

  • Clone repository or download it manually

    git clone https://github.com/xbersoy/nodejs-mongo-rest-api
  • Install dependencies

    npm install
  • Start the application

    npm run start

    or with nodemon

    npm run watch
  • Create a file name .env and specify port and database connection URL. Your .env file should look like below:

    PORT = 3000
    DB_CONNECTION_STRING = mongodb+srv://...

    That's all! Application should be running on your local.

    To run tests

    npm test

Install with docker

docker container run 
  -p <desired_port>:3000 burakersoy/nodejs-mongo-rest-api 

Swagger

You can test see and test endpoint with swagger ui.
http://ec2-3-141-198-45.us-east-2.compute.amazonaws.com:8080/api-docs



AWS

Application has been deployed to aws and the container runs on a ec2 instance. You can use the link below. http://ec2-3-141-198-45.us-east-2.compute.amazonaws.com:8080/


Test Result


More tests can be added

Endpoints

POST /records

URL: http://ec2-3-141-198-45.us-east-2.compute.amazonaws.com:8080/records (Only post request handled.)

Returns data from MongoDB with parameters given in request body.

With curl

curl -X POST -H "Content-Type: application/json"     -d '{
"startDate": "2016-01-26",
"endDate": "2016-02-02",
"minCount": 2700,
"maxCount": 3000
}'     http://ec2-3-141-198-45.us-east-2.compute.amazonaws.com:8080/records

(With tools like Postman)
Sample Request Payload

{
"startDate": "2016-01-26",
"endDate": "2016-02-02",
"minCount": 2700,
"maxCount": 3000
}

Successful Response Payload

{
    "code": 0,
    "msg": "Success",
    "records": [
        {
            "key": "bxoQiSKL",
            "createdAt": "2016-01-29T01:59:53.494Z",
            "totalCount": 2991
        },
        {
            "key": "NOdGNUDn",
            "createdAt": "2016-01-28T07:10:33.558Z",
            "totalCount": 2813
        }
    ]
}

Invalid Request Payload - 1

{
"startDate": "2016-01-26",
"endDate": "2016-02-02",
"minCount": "2700",
"maxCount": 3000
}

Error Response Payload - 1

{
    "code": 2,
    "msg": "Validation error - Invalid request parameters.",
    "details": [
        {
            "message": "minCount should be a number, not a string",
            "param": "minCount"
        }
    ]
}

Invalid Request Payload - 2

{
"startDate": "2016-01-26",
"endDate": "2016-02-02",
"minCount": "",
"maxCount": 3000
}

Error Response Payload - 2

{
    "code": 2,
    "msg": "Validation error - Invalid request parameters.",
    "details": [
        {
            "message": "minCount can not be empty!",
            "param": "minCount"
        }
    ]
}

Invalid Request Payload - 3

{
"startDate": "2016-01-26",
"endDate": "2016-02-02",
"minCount": 3200,
"maxCount": 3000
}

Error Response Payload - 3

{
    "code": 2,
    "msg": "Validation error - Invalid request parameters.",
    "details": [
        {
            "message": "minCount should be greater than maxCount",
            "param": "minCount"
        },
        {
            "message": "maxCount should be greater than minCount",
            "param": "maxCount"
        }
    ]
}

Invalid Request Payload - 4

{
"startDate": "2016-05asd-26",
"endDate": "2016-02-02",
"minCount": 2700,
"maxCount": 3000
}

Error Response Payload - 4

{
    "code": 2,
    "msg": "Validation error - Invalid request parameters.",
    "details": [
        {
            "message": "startDate must be a valid date",
            "param": "startDate"
        }
    ]
}

Invalid Request Payload - 5

{
"startDate": "2016-05-26",
"endDate": "2016-02-02",
"minCount": 2700,
"maxCount": 3000
}

Error Response Payload - 5

{
    "code": 2,
    "msg": "Validation error - Invalid request parameters.",
    "details": [
        {
            "message": "startDate should be less than endDate",
            "param": "startDate"
        }
    ]
}

Invalid endpoint sample - 6
URL : localhost:3000/test

{

}

Invaild endpoint response payload - 6

{
    "code": 3,
    "msg": "Route does not exist"
}

About

NodeJS based rest service application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published