-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (38 loc) · 1.43 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require('dotenv').config(); // Dodaj to na początku pliku
const express = require('express');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const mongoose = require('mongoose');
const register = require('./Controllers/register');
const signin = require('./Controllers/signin');
const profile = require('./Controllers/profile');
const image = require('./Controllers/image');
// Połączenie z MongoDB
mongoose.connect(process.env.DATABASE_URL)
.then(() => console.log('Connected to MongoDB'))
.catch(err => {
console.error('Error connecting to MongoDB:', err);
process.exit(1); // Zakończ aplikację w przypadku błędu połączenia
});
// Dodanie obsługi zdarzeń dla połączenia MongoDB
mongoose.connection.on('error', err => {
console.error('MongoDB connection error:', err);
});
mongoose.connection.on('disconnected', () => {
console.log('MongoDB disconnected');
});
process.on('SIGINT', async () => {
await mongoose.connection.close();
process.exit(0);
});
const app = express();
app.use(cors());
app.use(express.json());
app.get('/', (req, res) => { res.send('it is working') });
app.post('/signin', signin.handleSignin(bcrypt));
app.post('/register', register.handleRegister(bcrypt));
app.get('/profile/:id', profile.handleProfileGet);
app.put('/image', image.handleImage);
app.post('/imageurl', image.handleApiCall);
const port = 3001;
app.listen(port, () => { console.log(`App is running on port ${port}`) });