-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (32 loc) · 977 Bytes
/
server.js
File metadata and controls
46 lines (32 loc) · 977 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
46
const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const cors = require('cors');
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
const mongoURI = process.env.MONGO_URI;
if (!mongoURI) {
console.error("FATAL ERROR: MONGO_URI is not defined in the .env file.");
process.exit(1);
}
mongoose.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Successfully connected to MongoDB!'))
.catch(err => {
console.error('Database connection error:', err);
process.exit(1);
});
const taskRoutes = require('./routes/tasks');
app.use('/api/tasks', taskRoutes);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});