-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (50 loc) · 1.77 KB
/
index.js
File metadata and controls
61 lines (50 loc) · 1.77 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
// import the necessary modules
const sequelize = require('./config/db');
// Admin create function
const createAdmin = require('./config/admin');
// import the routes
const userRoutes = require('./routes/user.routes');
const candidateRoutes = require('./routes/candidate.routes');
const electionRoutes = require('./routes/election.routes');
const voteRoutes = require('./routes/vote.routes');
const voteControlRoutes = require('./routes/voteControl.routes');
const app = express();
app.use(bodyParser.json());
const port = 5000;
// Use CORS middleware
app.use(
cors({
origin: 'http://localhost:3000', // Allow only requests from this origin
methods: ['GET', 'POST', 'PUT', 'DELETE'], // Specify allowed methods if needed
credentials: true, // Include this if you need to send cookies or other credentials
})
);
app.get('/', (req, res) => {
// run the coomand "npm run dev" start backend using the nodemon
res.send('This is BallotGo Backend!');
});
// use the routes
app.use('/api/user', userRoutes);
app.use('/api/candidate', candidateRoutes);
app.use('/api/election', electionRoutes);
app.use('/api/vote', voteRoutes);
app.use('/api/control', voteControlRoutes);
try {
sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
// sequelize
// .sync({ force: true })
// .then(() => {
// createAdmin();
// console.log('Database connected and models synchronized');
// })
// .catch((err) => console.error('Error connecting to the database:', err));
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});