Skip to content

API Bate Papo UOL - A backend application developed using NodeJS and mongoDB

Notifications You must be signed in to change notification settings

Lucas-zz/BatePapoUOL_BackEnd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BatePapoUOL BackEnd

My first backend project! Building an API to the most used brazillian chat app ages ago - Bate Papo UOL!

Table of Contents

Overview

You can make the following requests to the API:

  • POST /participants

Set a username in the data base, you can send a .json body like this:

{
  name: "YOUR_NAME_HERE"
}

  • GET /participants

Get an array of every participant in the data base. There will be an ID, the username and a timestamp Date.now() informing the user last status in the following way:

{
  _id: new ObjectId(NUMBER),
  name: "USERNAME",
  lastStatus: NUMBER
}

  • POST /messages

Insert a message in the collection of messages, specifing to whom the message is being sent*, its type ("private_message", "message", "status") and the text. You can send a .json body like this:

{
  to: "HEADERS.USER",
  type: "TYPE_OF_MESSAGE",
  text: "MESSAGE"
}

*You'll need to inform the receiver of the message as User in the request's headers:

request.post('EXAMPLE_URL/messages', body, {
  headers: {
    User: RECEIVER
  }
});

  • GET /messages

Get an array of objects with all messages in the collection of messages with the sender, the receiver, the type of message ("private_message", "message", "status"), the text and the timestamp of the message (HH:mm:ss) in the following way:

  [
    {
      from: "SENDER",
      to: "RECEIVER",
      type: "TYPE_OF_MESSAGE",
      text: "MESSAGE",
      time: TIME
    }
  ]

  • DELETE /messages/:id

Delete a message sending its ObjectId in the request URL and the username (only the sender of a message is able to delete it) in the request's headers as User, like this:

request.delete(`EXAMPLE_URL/messages/${id}`, {
  headers: {
    User: SENDER
  }
});

  • PUT /messages/:id

Request to change a specific message in the collection of messages informing the messages' ObjectId in the URL. The API will be waiting for the same type of information sent in the POST /messages and the User (only the sender of said message is able to update it) in the headers of the request, like this:

request.put(`EXEMPLE_URL/messages/${id}`,
  {
    to: RECEIVER,
    text: "MESSAGE",
    type: "TYPE_OF_MESSAGE"
  }, {
    headers: {
      User: SENDER
    }
  }
);

  • POST /status

Every 15 seconds, the API will check the lastStatus of every user for a status that wasn't updated in the last 10 seconds and if confirmed, it'll remove the user from the DB. With this route, it's possible to keep updating the user timestamp (lastStatus). Like before, the User needs to be specified through the request's headers.

Data Sanitization

The API will automatically remove HTML tags and unnecessary white spaces from messages and usernames by default. 8)


Built with

The following tools and libs were used in the construction of the project:

My workspace:

How to run

You will need:

  • npm
  • NodeJS
  • MongoDB

Steps:

  1. Clone this repository

  2. Install dependencies

npm i
  1. Start the server
mongod --dbpath ~/.mongo

For more information about MongoDB, access its Documentation.

  1. Start the data base app*
mongo
mongosh

*Obs: open ANOTHER terminal and use one of the commands above (don't close or change the 3rd step terminal)

  1. Get the URL shown on Connected to: mongodb://XXX.X.X.X:XXXXX/ and copy it into the .env file as MONGO_URI=<URL>

  2. Finally, start doing requests to the server

  3. Alternatively, if you want a front-end app to see the API working, you can use:

https://github.com/Lucas-zz/BatePapoUOL_FrontEnd

Author

About

API Bate Papo UOL - A backend application developed using NodeJS and mongoDB

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published