-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'CHE-167/story/BE-Refactor-Error-Handling' of https://gi…
…thub.com/Code-Hammers/code-hammers into CHE-167/story/BE-Refactor-Error-Handling
- Loading branch information
Showing
9 changed files
with
120 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import path from 'path'; | ||
import express, { Request, Response } from 'express'; | ||
import 'express-async-errors'; | ||
import dotenv from 'dotenv'; | ||
import cookieParser from 'cookie-parser'; | ||
dotenv.config(); | ||
import { | ||
userRouter, | ||
profileRouter, | ||
authRouter, | ||
imageRouter, | ||
alumniRouter, | ||
forumRouter, | ||
devRouter, | ||
} from './routes'; | ||
import errorHandler from './middleware/errorHandler'; | ||
import { NotFoundError } from './errors'; | ||
|
||
// Instantiate application | ||
const app = express(); | ||
|
||
// Middleware to parse request bodies | ||
app.use(express.json()); | ||
// Middleware to parse request cookies | ||
app.use(cookieParser()); | ||
|
||
// API routers | ||
app.use('/api/users', userRouter); | ||
app.use('/api/profiles', profileRouter); | ||
app.use('/api/auth', authRouter); | ||
app.use('/api/images', imageRouter); | ||
app.use('/api/alumni', alumniRouter); | ||
app.use('/api/forums', forumRouter); | ||
app.use('/api/devRoutes', devRouter); | ||
|
||
// Serve client from build in production | ||
if (process.env.NODE_ENV === 'production') { | ||
console.log(`SERVER STARTED IN PRODUCTION`); | ||
app.use(express.static(path.join(__dirname, '../../client/build'))); | ||
|
||
app.get('*', (req: Request, res: Response) => | ||
res.sendFile(path.resolve(__dirname, '../../client/build/index.html')), | ||
); | ||
} | ||
|
||
// Catch all route handler | ||
app.use((_req, _res) => { | ||
throw new NotFoundError(); | ||
}); | ||
|
||
// Global error handler | ||
app.use(errorHandler); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,32 @@ | ||
import path from 'path'; | ||
import express, { Request, Response, Application } from 'express'; | ||
import 'express-async-errors'; | ||
import userRoutes from './routes/userRoutes'; | ||
import profileRoutes from './routes/profileRoutes'; | ||
import authRoutes from './routes/authRoutes'; | ||
import imageRoutes from './routes/imageRoutes'; | ||
import alumniRoutes from './routes/alumniRoutes'; | ||
import forumRoutes from './routes/forumRoutes'; | ||
import devRoutes from './routes/devRoutes'; | ||
import applicationsRoutes from './routes/applicationsRoutes'; | ||
import app from './app'; | ||
import connectDB from './config/db'; | ||
import dotenv from 'dotenv'; | ||
import cookieParser from 'cookie-parser'; | ||
import errorHandler from './middleware/errorHandler'; | ||
import { NotFoundError } from './errors'; | ||
|
||
dotenv.config(); | ||
|
||
const app: Application = express(); | ||
|
||
app.use(express.json()); | ||
app.use(cookieParser()); | ||
|
||
app.get('/health', (req: Request, res: Response) => { | ||
res.status(200).send('OK'); | ||
}); | ||
|
||
app.use('/api/users', userRoutes); | ||
app.use('/api/profiles', profileRoutes); | ||
app.use('/api/auth', authRoutes); | ||
app.use('/api/images', imageRoutes); | ||
app.use('/api/alumni', alumniRoutes); | ||
app.use('/api/forums', forumRoutes); | ||
app.use('/api/devRoutes', devRoutes); | ||
app.use('/api/applications', applicationsRoutes); | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
console.log(`SERVER STARTED IN PRODUCTION`); | ||
app.use(express.static(path.join(__dirname, '../../client/build'))); | ||
|
||
app.get('*', (req: Request, res: Response) => | ||
res.sendFile(path.resolve(__dirname, '../../client/build/index.html')), | ||
); | ||
} else { | ||
console.log('SERVER STARTED IN DEV'); | ||
app.get('/api', (req: Request, res: Response) => { | ||
res.json({ message: 'API Running - Hazzah!' }); | ||
}); | ||
} | ||
|
||
app.use((_req, _res) => { | ||
throw new NotFoundError(); | ||
}); | ||
|
||
app.use(errorHandler); | ||
|
||
const PORT: number = Number(process.env.PORT) || 3000; | ||
|
||
export const startServer = () => { | ||
connectDB(); | ||
|
||
// Hazzah! | ||
const hazzah = process.env.NODE_ENV === 'development' ? 'Hazzah! ' : ''; | ||
|
||
export const startServer = async () => { | ||
// Environment variable checks | ||
if (!process.env.JWT_SECRET) throw Error('❌ JWT_SECRET must be defined!'); | ||
if (!process.env.MONGO_URI) throw Error('❌ MONGO_URI must be defined!'); | ||
if (!process.env.POSTGRES_USER) throw Error('❌ POSTGRES_USER must be defined!'); | ||
if (!process.env.POSTGRES_DB) throw Error('❌ POSTGRES_DB must be defined!'); | ||
if (!process.env.POSTGRES_PASSWORD) throw Error('❌ POSTGRES_PASSWORD must be defined!'); | ||
if (!process.env.AWS_ACCESS_KEY_ID) throw Error('❌ AWS_ACCESS_KEY_ID must be defined!'); | ||
if (!process.env.AWS_SECRET_ACCESS_KEY) throw Error('❌ AWS_SECRET_ACCESS_KEY must be defined!'); | ||
if (!process.env.AWS_REGION) throw Error('❌ AWS_REGION must be defined!'); | ||
if (!process.env.BUCKET_NAME) throw Error('❌ BUCKET_NAME must be defined!'); | ||
|
||
// Connect to MongoDB | ||
await connectDB(process.env.MONGO_URI); | ||
|
||
// Startup the server | ||
return app.listen(PORT, () => | ||
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`), | ||
console.log(`💥 ${hazzah}Server running in ${process.env.NODE_ENV} mode on port ${PORT}`), | ||
); | ||
}; | ||
|
||
if (require.main === module) { | ||
startServer(); | ||
} | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import alumniRouter from './alumniRoutes'; | ||
import authRouter from './authRoutes'; | ||
import forumRouter from './forumRoutes'; | ||
import imageRouter from './imageRoutes'; | ||
import profileRouter from './profileRoutes'; | ||
import userRouter from './userRoutes'; | ||
import devRouter from './devRoutes'; | ||
|
||
export { alumniRouter, authRouter, forumRouter, imageRouter, profileRouter, userRouter, devRouter }; |