Skip to content

Commit d5b2233

Browse files
committed
Convert all ts to js because compilation is hard
1 parent ba98cf9 commit d5b2233

File tree

10 files changed

+85
-97
lines changed

10 files changed

+85
-97
lines changed

collab-service/Dockerfile

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@ FROM node:20-alpine AS base
33
WORKDIR /app
44
COPY package*.json ./
55

6+
# Install dependencies in base stage
7+
RUN npm install
8+
69
# Development stage
710
FROM base AS dev
8-
RUN npm install
911
COPY . .
10-
# Run the TypeScript code directly in dev mode (useful for hot-reloading)
12+
# Run the JavaScript code directly in dev mode (useful for hot-reloading)
1113
CMD ["npm", "run", "dev"]
1214

13-
# Build stage
14-
FROM base AS build
15-
RUN npm install
16-
COPY . .
17-
RUN npm run build
18-
1915
# Production stage
2016
FROM node:20-alpine AS prod
2117
WORKDIR /app
22-
COPY --from=build /app/dist ./dist
23-
COPY package*.json ./
24-
RUN npm ci --only=production
25-
CMD ["node", "dist/index.js"]
18+
COPY --from=base /app/node_modules ./node_modules
19+
COPY . .
20+
# Start the server in production mode
21+
CMD ["npm", "start"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export async function create_room(req, res) {
2+
// this function is to create a connection between two users
3+
// the connection is store in mongodb
4+
5+
const { user1, user2 } = req.body;
6+
7+
}

collab-service/app/controller/collab-controller.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

collab-service/app/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import express from 'express';
2+
import cors from 'cors';
3+
import collabRoutes from './routes/collab-routes.js';
4+
5+
const app = express();
6+
7+
app.use(express.urlencoded({ extended: true }));
8+
app.use(express.json());
9+
app.use(cors()); // config cors so that front-end can use
10+
app.options("*", cors());
11+
12+
13+
// To handle CORS Errors
14+
app.use((req, res, next) => {
15+
res.header("Access-Control-Allow-Origin", "*"); // "*" -> Allow all links to access
16+
17+
res.header(
18+
"Access-Control-Allow-Headers",
19+
"Origin, X-Requested-With, Content-Type, Accept, Authorization",
20+
);
21+
22+
// Browsers usually send this before PUT or POST Requests
23+
if (req.method === "OPTIONS") {
24+
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH");
25+
return res.status(200).json({});
26+
}
27+
28+
// Continue Route Processing
29+
next();
30+
});
31+
32+
33+
app.get("/", (req, res, next) => {
34+
console.log("Sending Greetings!");
35+
res.json({
36+
message: "Hello World from collab-service",
37+
});
38+
});
39+
40+
// Handle When No Route Match Is Found
41+
app.use((req, res, next) => {
42+
const error = new Error("Route Not Found");
43+
error.status = 404;
44+
next(error);
45+
});
46+
47+
app.use((error, req, res, next) => {
48+
res.status(error.status || 500);
49+
res.json({
50+
error: {
51+
message: error.message,
52+
},
53+
});
54+
});
55+
56+
app.use("/collab", collabRoutes);
57+
58+
export default app;

collab-service/app/index.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

collab-service/app/model/repository.ts renamed to collab-service/app/model/repository.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export async function connectToMongo() {
66
await connect('mongodb+srv://admin:[email protected]/');
77
}
88

9-
export async function createRoom(user1: string, user2: string, roomId: string) {
9+
export async function createRoom(user1, user2, roomId) {
1010
try {
1111
const newRoom = new UsersSession({
1212
users: [user1, user2],
13-
roomId: new mongoose.Types.ObjectId().toString(), // Generate a unique room ID
13+
roomId: roomId,
1414
lastUpdated: new Date()
1515
});
1616

@@ -22,7 +22,7 @@ export async function createRoom(user1: string, user2: string, roomId: string) {
2222
}
2323
}
2424

25-
export async function get_roomID(user: string): Promise<mongoose.Document | null> {
25+
export async function get_roomID(user) {
2626
try {
2727
const room = await UsersSession.findOne({ users: user });
2828
return room;
File renamed without changes.

collab-service/app/routes/collab-routes.ts renamed to collab-service/app/routes/collab-routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from "express";
22

3-
import { create_room } from "../controller/collab-controller";
3+
import { create_room } from "../controller/collab-controller.js";
44

55
const router = express.Router();
66

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { connectToMongo } from "./model/repository";
1+
import { connectToMongo } from "./model/repository.js";
22
import http from "http";
3-
import { index } from "./index.ts";
3+
import index from "./index.js";
44
const PORT = process.env.PORT || 5000;
55
const server = http.createServer(index);

collab-service/package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
"name": "collab-service",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "index.js",
5+
"main": "app/index.js",
6+
"type": "module",
67
"scripts": {
8+
"dev": "nodemon -L app/index.js",
9+
"start": "node app/index.js",
710
"test": "echo \"Error: no test specified\" && exit 1"
811
},
912
"keywords": [],
@@ -15,10 +18,6 @@
1518
"mongoose": "^8.8.0"
1619
},
1720
"devDependencies": {
18-
"@types/express": "^5.0.0",
19-
"@types/mongoose": "^5.11.97",
20-
"@types/node": "^22.8.6",
21-
"ts-node": "^10.9.2",
22-
"typescript": "^5.6.3"
21+
"nodemon": "^3.1.4"
2322
}
24-
}
23+
}

0 commit comments

Comments
 (0)