From ff5b993b4a5aff6df8384750cc99de35f3b68be5 Mon Sep 17 00:00:00 2001 From: vedantterse Date: Mon, 24 Jun 2024 13:25:31 +0530 Subject: [PATCH] dockerized updated --- Backend/index.js | 84 +++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/Backend/index.js b/Backend/index.js index 35435fb..090ce83 100644 --- a/Backend/index.js +++ b/Backend/index.js @@ -1,47 +1,52 @@ -import express from 'express'; -import mongoose from 'mongoose'; -import Stripe from 'stripe'; -import cors from 'cors'; -import dotenv from 'dotenv'; -import bodyParser from 'body-parser'; -dotenv.config({ path: './.env' }); +import express from "express"; +import mongoose from "mongoose"; +import Stripe from "stripe"; +import cors from "cors"; +import dotenv from "dotenv"; +import bodyParser from "body-parser"; +dotenv.config({ + path: "./.env", +}); -import router from './Routes/user-routes.js'; -import routers from './Routes/product-routes.js'; -import routerss from './Routes/dashboard-routes.js'; -import routersss from './Routes/dashboardagent-routes.js'; -import routerAddress from './Routes/user-address-routes.js'; -import visitCountRoutes from './Routes/visit-count-routes.js'; +import router from "./Routes/user-routes.js"; +import routers from "./Routes/product-routes.js"; +import routerss from "./Routes/dashboard-routes.js"; +import routersss from "./Routes/dashboardagent-routes.js"; +import routerAddress from "./Routes/user-address-routes.js"; +import visitCountRoutes from "./Routes/visit-count-routes.js"; const app = express(); -const stripe = new Stripe(process.env.STRIPE_SECRET_KEY || "sk_test_51O96wfSH8i1UqUchc81vmn8Mka2bbbMrCW2vZKLEvGRTZDqWx2KlxkbLzdQnAJ0ipNA1UtO9Y83vX4x7KXjz5E4Z00JxrbAflY"); +const stripe = new Stripe( + "sk_test_51O96wfSH8i1UqUchc81vmn8Mka2bbbMrCW2vZKLEvGRTZDqWx2KlxkbLzdQnAJ0ipNA1UtO9Y83vX4x7KXjz5E4Z00JxrbAflY" +); + -// Increasing limit to store image in database -app.use(bodyParser.json({ limit: '10mb' })); -app.use(bodyParser.urlencoded({ limit: '10mb', extended: true })); +//incresing limit to store image in database +app.use(bodyParser.json({ limit: '10mb' })); +app.use(bodyParser.urlencoded({ limit: '10mb', extended: true })); app.use(cors()); app.use(express.json()); -app.use('/ecommerce/user', router); -app.use('/ecommerce/product', routers); -app.use('/ecommerce/manager', routerss); -app.use('/ecommerce/agent', routersss); -app.use('/ecommerce/user-address', routerAddress); -app.use('/visitcount', visitCountRoutes); // added newly +app.use("/ecommerce/user", router); +app.use("/ecommerce/product", routers); +app.use("/ecommerce/manager", routerss); +app.use("/ecommerce/agent", routersss); +app.use("/ecommerce/user-address", routerAddress); +app.use("/visitcount", visitCountRoutes); // added newly -app.post('/checkout', async (req, res) => { +app.post("/checkout", async (req, res) => { try { const { items } = req.body; if (!Array.isArray(items) || items.length === 0) { - return res.status(400).json({ error: 'Invalid or empty item data' }); + return res.status(400).json({ error: "Invalid or empty item data" }); } const line_items = items.map((item) => ({ price_data: { - currency: 'inr', + currency: "inr", product_data: { - name: 'Product Name', + name: "Product Name", }, unit_amount: item.price * 100, }, @@ -49,15 +54,17 @@ app.post('/checkout', async (req, res) => { })); const session = await stripe.checkout.sessions.create({ - payment_method_types: ['card'], - mode: 'payment', + payment_method_types: ["card"], + mode: "payment", line_items, - success_url: 'https://spectastyle.vercel.app/success', - cancel_url: 'https://spectastyle.vercel.app/cancel', + success_url: "https://spectastyle.vercel.app/success", + cancel_url: "https://spectastyle.vercel.app/cancel", }); if (!session || !session.id) { - return res.status(500).json({ error: 'Failed to create a checkout session' }); + return res + .status(500) + .json({ error: "Failed to create a checkout session" }); } res.json({ url: session.url }); @@ -67,12 +74,15 @@ app.post('/checkout', async (req, res) => { }); const port = process.env.PORT || 4000; -const mongoUri = process.env.MONGO_URL || 'mongodb://localhost:27017/ecommerce'; +const url = process.env.MONGO_URL; -mongoose.connect(mongoUri, { - useNewUrlParser: true, - useUnifiedTopology: true, -}) +mongoose + .connect(url, { + useNewUrlParser: true, + useUnifiedTopology: true, + }) .then(() => app.listen(port)) .then(() => console.log(`connected to db at port ${port} :)`)) .catch((err) => console.log(`${err} is error`)); + +// updated with the ports \ No newline at end of file