My first backend
project! Building an API to the most used brazillian chat app ages ago - Bate Papo UOL!
- 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.
The API will automatically remove HTML tags and unnecessary white spaces from messages and usernames by default. 8)
The following tools and libs were used in the construction of the project:
My workspace:
- npm
- NodeJS
- MongoDB
-
Clone this repository
-
Install dependencies
npm i
- Start the server
mongod --dbpath ~/.mongo
For more information about MongoDB, access its Documentation.
- 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)
-
Get the URL shown on
Connected to: mongodb://XXX.X.X.X:XXXXX/
and copy it into the .env file asMONGO_URI=<URL>
-
Finally, start doing requests to the server
-
Alternatively, if you want a front-end app to see the API working, you can use:
- LinkedIn - Lucas Azzolini Vieira
- Frontend Mentor - @Lucas-zz
- Gmail - [email protected]