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.
53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
|
|
// import dependenciees
|
|
const express = require('express');
|
|
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 courseRoutes = require('./routes/course');
|
|
|
|
// server setup
|
|
const app = express();
|
|
const port = 4000;
|
|
|
|
// Midlewares
|
|
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({extended:true}));
|
|
|
|
// Allows all resources to access our backend application
|
|
app.use(cors());
|
|
|
|
|
|
// database connection
|
|
mongoose.connect("mongodb+srv://justineyung02:Miras1607@cluster0.mymbzfm.mongodb.net/CourseBookingAPI?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("/users", userRoutes);
|
|
app.use('/courses',courseRoutes);
|
|
// server start
|
|
if(require.main === module)
|
|
app.listen( port, () => console.log(`Server running at port ${port}`));
|
|
|
|
module.exports = app;
|