Skip to content

Commit b8224b0

Browse files
author
JVancata
committed
add chat api
1 parent 3c3cc86 commit b8224b0

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

apps/chat-api/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:24.8-alpine
2+
3+
ENV NODE_ENV=production
4+
5+
COPY package.json .
6+
RUN npm install
7+
8+
COPY . .
9+
10+
CMD ["node", "index.ts"]
11+
EXPOSE 3000

apps/chat-api/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import express from 'express'
2+
import cors from 'cors'
3+
4+
const app = express()
5+
const port = 3000
6+
7+
app.use(cors())
8+
app.use(express.json());
9+
10+
type Message = {
11+
username: string,
12+
message: string
13+
}
14+
15+
const messages: Message[] = [];
16+
17+
app.get('/', (_req, res) => {
18+
res.json(messages);
19+
})
20+
21+
app.post("/", (req, res) => {
22+
const contentTypeHeader = req.headers["content-type"];
23+
if (contentTypeHeader !== "application/json") {
24+
res.status(415);
25+
res.send('Error: Content-Type must be "application/json"')
26+
return;
27+
}
28+
29+
const { username, message } = req.body;
30+
if (!username || !message) {
31+
res.status(422);
32+
res.send('Error: Body of the request is not complete, correct value is: {"username": "User", "message": "Hello!"}');
33+
return;
34+
}
35+
36+
if (typeof username !== "string" || typeof message !== "string") {
37+
res.status(422);
38+
res.send("Error: Username and message have to be a string");
39+
return;
40+
}
41+
42+
messages.push({ username, message });
43+
44+
res.json(messages);
45+
return;
46+
})
47+
48+
app.listen(port, () => {
49+
console.log(`chat-api running on port ${port}`)
50+
})

apps/chat-api/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "chat-api",
3+
"version": "1.0.0",
4+
"main": "index.ts",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "node --watch --experimental-strip-types index.ts",
8+
"docker:build": "docker build -t chat-api .",
9+
"docker:run": "docker run -d -p 3000:3000 chat-api"
10+
},
11+
"author": "",
12+
"license": "ISC",
13+
"description": "",
14+
"dependencies": {
15+
"cors": "^2.8.5",
16+
"express": "^5.1.0"
17+
},
18+
"devDependencies": {
19+
"@types/cors": "^2.8.18",
20+
"@types/express": "^5.0.2"
21+
}
22+
}

0 commit comments

Comments
 (0)