Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate server ts #497

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
dist
33 changes: 0 additions & 33 deletions server/index.js

This file was deleted.

56 changes: 56 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { mongoConnect } from './schema/mongo.connect';
import { cfpRouter } from './src/cpfs/cfp.router';
import { eventRouter } from './src/events/events.router';
import { hackathonRouter } from './src/hackathons/hackathon.router';
import { userRouter } from './src/users/user.router';
import * as express from 'express';
// eslint-disable-next-line @typescript-eslint/no-var-requires
// const express = require('express');
import { Express, NextFunction, Request, Response } from 'express';
import { RateLimiterMemory } from 'rate-limiter-flexible';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const dotenv = require('dotenv');

dotenv.config({ path: '.env' });

const app: Express = express();
const port = process.env.PORT;

const opts = {
points: 5, // Number of points
duration: 1, // Per second
};

const rateLimiter = new RateLimiterMemory(opts);

app.use((req: Request, res: Response, next: NextFunction) => {
rateLimiter
.consume(req.ip)
.then(() => {
next();
})
.catch(() => {
res.status(429).send({ success: false, message: 'Too Many Requests' });
});
});

app.use(express.json());

app.use('/api/user', userRouter);
app.use('/api/events', eventRouter);
app.use('/api/hackathons', hackathonRouter);
app.use('/api/cfps', cfpRouter);

async function startServer() {
try {
await mongoConnect();
// eslint-disable-next-line no-console
app.listen(port, () => console.log(`Server is running on port ${port}`));
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error in server:', error);
process.exit(1); // Terminate the process on server startup failure
}
}

startServer();
Loading