-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
33 lines (27 loc) · 886 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
require('dotenv').config();
// Initialize Express app
const app = express();
const port = process.env.PORT || 5010; // Changed to 5001
// Use CORS middleware
app.use(cors({
origin: 'http://localhost:3000', // or '*' to allow all origins
methods: 'GET,POST,PUT,DELETE',
credentials: true
}));
// Middleware to parse JSON
app.use(express.json());
// Connect to MongoDB
const uri = process.env.ATLAS_URI;
mongoose.connect(uri)
.then(() => console.log("MongoDB database connection established successfully"))
.catch(err => console.log("MongoDB connection error:", err));
// Define routes
const booksRouter = require('./routes/books');
app.use('/books', booksRouter);
// Start the server
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});