-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (32 loc) · 1.04 KB
/
server.js
File metadata and controls
41 lines (32 loc) · 1.04 KB
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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const { SECRET } = require('./config');
const taskRoutes = require('./routes/taskRoutes');
const userRoutes = require('./routes/userRoutes').router;
const app = express();
// Middleware
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(__dirname)); // Obsługuje statyczne pliki frontendowe
// Połączenie z MongoDB
mongoose.connect('mongodb://localhost:27017/todolist', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Loguj błędy MongoDB
mongoose.connection.on('error', (err) => {
console.error('MongoDB error:', err);
});
mongoose.connection.once('open', () => {
console.log('Connected to MongoDB');
});
// Trasy API
app.use('/api/tasks', taskRoutes);
app.use('/api/users', userRoutes);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
// Start serwera
app.listen(3000, () => console.log('Server running on http://localhost:3000'));