Giter Site home page Giter Site logo

batepapouol_backend's Introduction

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

batepapouol_backend's People

Contributors

lucas-zz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

leleora

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.