Skip to content

Jotabustos/Nodepop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nodepop API

Nodepop is a REST API server implementation built on top Node.js and Express.js with Mongoose.js for MongoDB integration.

Running project

Manual

You need to have Node.js and MongoDB installed.

MongoDB service start up

# Go to the installation folder and run
./bin/mongod --dbpath ./data/db --directoryperdb

Install the required packages

npm install

Run server

npm start
# alias for
node bin/www

Run server in development mode

# Install development command
npm i cross-env --save--dev
# To start the application in development mode use:
npm run dev

Run server in cluster mode

# Start cluster mode
npm run cluster

Create demo data

In order to have some data, run the following command to initiate the database.

# Restart the database with the original demo data:
npm run startUpDB

Register

The first thing you need to do before using the API, is to register to create an account.

Do a POST request sending the following fields:

  • email - Enter a valid email
  • password - Choose a password
  • name - Your name
http POST /api/users/register email=EMAIL_ENTERED password=PASSWORD_ENTERED name=NAME_ENTERED

After doing this, the API will answer sending a token that need to be send on the body on every request to access the data.

Answer of the API:

# Example of API answer with the token
{
    "success": true,
    "message": "User created succesfully",
    "token": "ey2JhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNWI0MWZkYTk0YTVlNTUzZTQ2NTY4NzEwIiwiaWF0IjoxNTMxMDUxNDMzLCJleHAiOjE1MzExMzc4MzN9.DyvPPmUea7r-M_Sr7hmpSwKSNAVXoFtCkJGKVwpQZo"
}
# Example of request with the token
GET /api/ads?sort=name&fields=name%20-_id&skip=3?token=ACCESS_TOKEN

or add the token to you headers:

x-access-token : ACCESS_TOKEN

Log in

If you are already registered, you need to log in with your credentials to get a valid token:

Do a POST request sending the following fields:

  • email - Your email
  • password - Your password
http POST localhost:3000/api/users/login email=EMAIL_ENTERED password=PASSWORD_ENTERED

After doing this, you will be given an access token as well.

# Example of API answer with the token
{
    "success": true,
    "token": "ey2JhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNWI0MWZkYTk0YTVlNTUzZTQ2NTY4NzEwIiwiaWF0IjoxNTMxMDUxNDMzLCJleHAiOjE1MzExMzc4MzN9.DyvPPmUea7r-M_Sr7hmpSwKSNAVXoFtCkJGKVwpQZo"
}

Make Requests

Get the data:

GET /api/ads?token=ACCESS_TOKEN

Pagination

To paginate results you can use:

# Skip results
?skip=NUMBER
# Example
GET /api/ads?skip=3&token=ACCESS_TOKEN
# Limit results
?limit=NUMBER
# Example
GET /api/ads?limit=2&token=ACCESS_TOKEN
# Sort results
?sort=FIELD
# Example
GET /api/ads?sort=name&token=ACCESS_TOKEN
# Get only a field
?fields=FIELD
# Get several fields
?fields=FIELD_1 FIELD_2
# Ignore object id
?fields=FIELD_1 -_id
# Example
GET /api/ads?fields=name&token=ACCESS_TOKEN
GET /api/ads?fields=name price&token=ACCESS_TOKEN
GET /api/ads?fields=name -_id&token=ACCESS_TOKEN
# Search by text. The results given are those that starts by the name or matches the same name given in a case insensitive way.
?FIELD_1=TEXT
# Example
GET /api/ads?name=audi&token=ACCESS_TOKEN
# Mixing
You can combine the previous commands
# Example
GET /api/ads?sort=name&fields=name%20-_id&skip=3&limit=10&token=ACCESS_TOKEN

Filter by price

# Find products with a minimum price
?price=NUMBER-
# Example
GET /api/ads?price=100-&token=ACCESS_TOKEN
# Find products with a maximum price
?price=-NUMBER
# Example
GET /api/ads?price=-100&token=ACCESS_TOKEN
# Find products in price range. NUMBER_1 should be the lowest
?price=NUMBER_1-NUMBER_2
# Example
GET /api/ads?price=100-300&token=ACCESS_TOKEN

Práctica del curso de DevOps

Ejercicio 1

URL de la API: https://nodepop.jotabustos.com

Ejercicio 2

  • IP del servidor: 18.209.170.168
  • También se puede acceder por HTTPS al mismo contenido en https://jotabustos.com

Author

Created and maintained by José Julián Bustos Díaz.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages