chat-app/
├── config/
│ └── database.js
├── controllers/
│ └── chatController.js
├── middleware/
│ └── authMiddleware.js
├── models/
│ └── Chat.js
├── routes/
│ └── chatRoutes.js
├── .env
├── package.json
├── server.js
└── README.md
mkdir chat-app
cd chat-app
npm init -y
npm install express mysql2 sequelize cors dotenv
node server.js
npm run dev
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
curl http://localhost:3000/api/chats | jq
curl -X GET http://localhost:3000/api/chats/999
curl -X PATCH http://localhost:3000/api/chats/1 -H "Content-Type: application/json" -d '{"message": "Updated message", "sender": "Updated sender"}'
curl -X DELETE http://localhost:3000/api/chats/1
curl -X POST http://localhost:3000/api/chats/dummy
http://localhost:3000/api/chats/
{
"code": 200,
"message": "Chats retrieved successfully",
"data": [
{
"id": 1,
"message": "Hello!",
"sender": "User1",
"createdAt": "2024-11-03T03:33:22.000Z",
"updatedAt": "2024-11-03T03:33:22.000Z"
},
{
"id": 2,
"message": "Hi there!",
"sender": "User2",
"createdAt": "2024-11-03T03:33:22.000Z",
"updatedAt": "2024-11-03T03:33:22.000Z"
},
{
"id": 3,
"message": "How are you?",
"sender": "User1",
"createdAt": "2024-11-03T03:33:22.000Z",
"updatedAt": "2024-11-03T03:33:22.000Z"
},
{
"id": 4,
"message": "I am good, thanks!",
"sender": "User2",
"createdAt": "2024-11-03T03:33:22.000Z",
"updatedAt": "2024-11-03T03:33:22.000Z"
}
]
}
- Headers :
* key: Content-Type
* value : application/json
{
"message": "halo",
"sender": "ugun"
}
http://localhost:3000/api/chats/