Skip to content

Commit

Permalink
Update app.js with optimized code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugunth140 authored Oct 19, 2024
1 parent 86b5a18 commit 0bf1486
Showing 1 changed file with 42 additions and 41 deletions.
83 changes: 42 additions & 41 deletions backend/app.js
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;

0 comments on commit 0bf1486

Please sign in to comment.