The chat-api is an API with send_message
and read_messages
endpoints. send_message
requests the user to specify a unique chatroom identifier (chat_id), sender name, and the message in order to save it in the database. read_messages
requests a chat_id and optionally the sender's name in order to fetch the messages contained in the unique chatroom.
- Node.js with npm
- MongoDB Atlas Account
- Log into your Atlas account
- Create a new cluster
- Choose allowed IPs
- Create a database account
- Choose
Connect your application
- Copy the provided
uri
- Download this project
- Run
npm install
in the project directory - Create a
.env
file in your project directory - Add
URI=your_uri_here
to the.env
file and save it - Run
npm start
in project terminal
You can try the API by interacting with https://acm-chat-api.herokuapp.com/
Apps on Heroku tend to sleep after being idle for some time which means it may take a while to initially process an API request.
POST /send_message
Eg. http://localhost:3000/send_message
{
"chat_id": "chatroom1"
"sender": "adam",
"message": "This is just a test message..."
}
=>
db_object_id
GET /read_messages
Eg. http://localhost:3000/read_messages
{
"chat_id": "chatroom1"
"sender": "adam" // optional
}
=>
[{
"sender": "adam",
"message": "This is just a test message..."
}]
const http = require('http');
var data = JSON.stringify({
chat_id: 'chatroom1',
sender: 'anonymous',
message: 'This is a test message...'
});
var options = {
hostname: 'acm-chat-api.herokuapp.com',
path: '/send_message',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
var req = http.request(options, res => {
res.on('data', d => {
console.log(`${d}`)
});
});
req.write(data);
req.end();