Skip to content

Commit

Permalink
adding express routing to backend (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
eriysd authored Nov 6, 2024
1 parent ddca11f commit 4d1a938
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cors, { CorsOptions } from 'cors';
import path from 'path';
import fs from 'fs';
import cameraRouter from './routes/cameraRoutes';
import gptRouter from './routes/gptRoutes';

// define directory to save images
const uploadDir = path.join(__dirname, '../__uploads');
Expand All @@ -22,6 +23,7 @@ app.use(express.json());

// routes
app.use('/camera', cameraRouter);
app.use('/gpt', gptRouter);

app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
Expand Down
25 changes: 25 additions & 0 deletions server/src/controllers/gptController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response } from 'express';
import { GptService } from '../services/gptService';

export const GptController = {
async ask(req: Request, res: Response) {
try {
if (!req.body) {
return res.status(400).json({ success: false, message: 'Empty body' });
}

let gptService = new GptService();
let data = gptService.doSomething();

res.status(200).json({ success: true, message: 'Reached the backend successfully', data });
} catch (err) {
if (err instanceof Error) {
res.status(500).json({ success: false, message: 'Failed to ask gpt', error: err.message });
} else {
res
.status(500)
.json({ success: false, message: 'Failed to ask gpt', error: 'Unknown error' });
}
}
},
};
8 changes: 8 additions & 0 deletions server/src/routes/gptRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from 'express';
import { GptController } from '../controllers/gptController';

const router = express.Router();

router.post('/ask', GptController.ask);

export default router;
6 changes: 6 additions & 0 deletions server/src/services/gptService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class GptService {
doSomething = () => {
console.log('inside the gpt service');
return "called gpt"
};
}

0 comments on commit 4d1a938

Please sign in to comment.