You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
12 months ago
|
|
||
|
// import dependenciees
|
||
|
const express = require('express');
|
||
|
// mongoose- come from the Mongodb
|
||
|
const mongoose = require('mongoose')
|
||
|
|
||
|
// CORS (Cross Origin Resource Sharing)
|
||
|
// Allow our backend application to be available to our frontend application
|
||
|
const cors = require('cors');
|
||
|
|
||
|
// Import routes
|
||
|
const userRoutes = require('./routes/user');
|
||
|
const productRoutes = require('./routes/product');
|
||
|
const orderRoutes = require('./routes/order');
|
||
|
|
||
|
|
||
|
// server setup
|
||
|
const app = express();
|
||
|
const port = 4003;
|
||
|
|
||
|
// Midlewares
|
||
|
|
||
|
app.use(express.json());
|
||
|
app.use(express.urlencoded({extended:true}));
|
||
|
|
||
|
// Allows all resources to access our backend application
|
||
|
// Assuming you're using Express
|
||
|
// app.use((req, res, next) => {
|
||
|
// res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
|
||
|
// // Additional headers can be set as needed
|
||
|
// next();
|
||
|
// });
|
||
|
app.use(cors());
|
||
|
|
||
|
|
||
|
// database connection
|
||
|
mongoose.connect("mongodb+srv://justineyung02:Miras1607@cluster0.mymbzfm.mongodb.net/EcommerceAPI?retryWrites=true&w=majority", {
|
||
|
useNewUrlParser: true,
|
||
|
useUnifiedTopology: true
|
||
|
});
|
||
|
|
||
|
// connecting to MongoDb locally if no internet connection
|
||
|
|
||
|
// mongoose.connect("mongodb://localhost:27017/b320-todo", {
|
||
|
// useNewUrlParser: true,
|
||
|
// useUnifiedTopology: true
|
||
|
// });
|
||
|
|
||
|
let db = mongoose.connection
|
||
|
db.on('error', console.error.bind(console, 'Connection error'));
|
||
|
db.once('open', () => console.log('Were connected to the cloud database'));
|
||
|
// Backend routes
|
||
|
// it should be plural as always
|
||
|
// the users coming from the filename name in the routes. it is jut plural because it is default to be plural.
|
||
|
app.use("/b3/users", userRoutes);
|
||
|
app.use("/b3/products", productRoutes);
|
||
|
app.use("/b3/orders", orderRoutes);
|
||
|
// server start
|
||
|
if(require.main === module)
|
||
|
app.listen( port, () => console.log(`Server running at port ${port}`));
|
||
|
module.exports = app;
|