server.js 3 KB
import bodyParser from "body-parser";
import cors from "cors";
import dotenv from "dotenv";
import express from "express";
import mongoose from "mongoose";
import multer from "multer";

//swagger doc
import swaggerJsdoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";

const swaggerOptions = {
  swaggerDefinition: {
    info: {
      title: "Sign Connect Plus API Documentation",
      version: "1.0.0",
      description: "Documentation for Sign Connect Plus Node Backend API",
    },
    basePath: "/rest_node", // Specify the base path of your API
  },
  apis: [
    "./routes/curriculum.routes.js",
    "./routes/feedback.routes.js",
    "./routes/leaderboard.routes.js",
    "./routes/marksCalculator.routes.js",
    "./routes/translate.routes.js",
    "./routes/tutorial.routes.js",
    "./routes/user.routes.js",
    "./routes/userProgress.routes.js",
  ],
};

const swaggerSpec = swaggerJsdoc(swaggerOptions);

// Set up storage for uploaded images
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });

//import routes
import curriculumRoutes from "./routes/curriculum.routes.js";
import feedbackRoutes from "./routes/feedback.routes.js";
import leaderboardRoutes from "./routes/leaderboard.routes.js";
import marksCalculatorRoutes from "./routes/marksCalculator.routes.js";
import translateRoutes from "./routes/translate.routes.js";
import tutorialRoutes from "./routes/tutorial.routes.js";
import userRoutes from "./routes/user.routes.js";
import userProgressRoutes from "./routes/userProgress.routes.js";

dotenv.config();
const app = express();

const corsOptions = {
  origin: 'http://localhost:3000',
  origin: 'http://localhost:3001',
};

app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use(cors(corsOptions));
//end

app.get("/", (req, res) => {
  res.json({ message: "Welcome to Server Node" });
});

//implement routes 
app.use("/rest_node/ssl", translateRoutes);
app.use("/rest_node/user", userRoutes);
app.use("/rest_node/curriculum", curriculumRoutes);
app.use("/rest_node/tutorial", tutorialRoutes);
app.use("/rest_node/user-progress", userProgressRoutes);
app.use("/rest_node/feedback", feedbackRoutes);
app.use("/rest_node/leaderboard", leaderboardRoutes);
app.use("/rest_node/marks-calculator", marksCalculatorRoutes);

app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));

const CONNECTION_URL = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@researchmanagement-appl.vzhn4.mongodb.net/?retryWrites=true&w=majority`;
const PORT = process.env.PORT || 5000;

mongoose
  .connect(CONNECTION_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    app.listen(PORT, () =>
      console.log(`Server Running on port :http://localhost:${PORT}`)
    );
  })
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send("Internal Server Error");
});