const express = require("express");
const mongoose = require("mongoose");

const userRoutes = require("./routes/userRoutes.js");
const productRoutes = require("./routes/productRoutes.js");
const cors = require("cors");

const port = 4002;

const app = express();

mongoose.connect('mongodb+srv://admin:admin@b330juan.sxv2kqg.mongodb.net/Capstone-2?retryWrites=true&w=majority');

let connect = mongoose.connection;

connect.on("error", console.error.bind(console, "Connection error!"));

connect.once("open", () => {
	console.log("Connected with the Database!");
});


// Middlewares
app.use(express.json());
app.use(express.urlencoded({extended : true}));
app.use(cors());
app.use("/b2/users", userRoutes);
app.use("/b2/products", productRoutes);



// run the server to a specific port:
app.listen(port, () => {
	console.log(`API is now online on port ${port}!`);
})
