-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update app.js with optimized code structure
- Loading branch information
1 parent
86b5a18
commit 0bf1486
Showing
1 changed file
with
42 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,54 @@ | ||
import dotenv from "dotenv"; | ||
import dotenv from 'dotenv'; | ||
import express from 'express'; | ||
import cookieParser from 'cookie-parser'; | ||
import cors from 'cors'; | ||
|
||
import express from "express"; | ||
import cookieParser from "cookie-parser"; | ||
import { errorHandler } from "./src/utilis/ApiError.js"; | ||
import cors from "cors"; | ||
import { errorHandler } from './src/utilis/ApiError.js'; // Custom error handler | ||
|
||
const app = express(); | ||
// Import all routes | ||
import userRouter from './src/routes/user.routes.js'; | ||
import contactUsRouter from './src/routes/contactus.routes.js'; | ||
import appointmentRouter from './src/routes/appointment.routes.js'; | ||
import medicineRouter from './src/routes/medicine.routes.js'; | ||
import CartRouter from './src/routes/UserCart.routes.js'; | ||
import PaymentRouter from './src/routes/payment.routes.js'; | ||
import TestimonialRouter from './src/routes/testimonial.routes.js'; | ||
|
||
// dotenv configuration | ||
dotenv.config({ | ||
path: "./.env", | ||
}); | ||
// Load environment variables | ||
dotenv.config({ path: './.env' }); | ||
|
||
const app = express(); | ||
|
||
// cors middleware configuration connects frontend to backend | ||
// Middleware configurations | ||
// Setup CORS to allow requests from trusted origins, including credentials support | ||
app.use( | ||
cors({ | ||
origin: [process.env.FRONTEND_URL, process.env.DASHBOARD_URL], | ||
method: ["GET", "POST", "DELETE", "PUT"], | ||
methods: ['GET', 'POST', 'DELETE', 'PUT'], | ||
credentials: true, | ||
}), | ||
}) | ||
); | ||
|
||
// Middleware for parsing cookies and request bodies | ||
app.use(cookieParser()); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
// import routes | ||
import userRouter from "./src/routes/user.routes.js"; | ||
import contactUsRouter from "./src/routes/contactus.routes.js"; | ||
import appointmentRouter from "./src/routes/appointment.routes.js"; | ||
import medicineRouter from "./src/routes/medicine.routes.js"; | ||
import CartRouter from "./src/routes/UserCart.routes.js"; | ||
import PaymentRouter from "./src/routes/payment.routes.js"; | ||
import TestimonialRouter from "./src/routes/testimonial.routes.js"; | ||
|
||
// Define the root route | ||
// app.get('/', (req, res) => { | ||
// res.send('Welcome to the homepage!'); | ||
// }); | ||
|
||
// routes declaration | ||
app.use("/api/v1/user", userRouter); | ||
app.use("/api/v1/message", contactUsRouter); | ||
app.use("/api/v1/appointment", appointmentRouter); | ||
app.use("/api/v1/medicines", medicineRouter); | ||
app.use("/api/v1/medicines-cart", CartRouter) | ||
app.use("/api/v1/payment", PaymentRouter) | ||
app.use("/api/v1/testimonial", TestimonialRouter) | ||
|
||
|
||
// error middleware | ||
app.use(express.json()); // Automatically parse JSON payloads | ||
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded data | ||
|
||
// API route handlers | ||
app.use('/api/v1/user', userRouter); | ||
app.use('/api/v1/message', contactUsRouter); | ||
app.use('/api/v1/appointment', appointmentRouter); | ||
app.use('/api/v1/medicines', medicineRouter); | ||
app.use('/api/v1/medicines-cart', CartRouter); | ||
app.use('/api/v1/payment', PaymentRouter); | ||
app.use('/api/v1/testimonial', TestimonialRouter); | ||
|
||
// Default route (optional for base-level check) | ||
app.get('/', (req, res) => { | ||
res.send('API is running successfully.'); | ||
}); | ||
|
||
// Error handling middleware for unified API error responses | ||
app.use(errorHandler); | ||
|
||
export default app; |