-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (50 loc) · 1.63 KB
/
index.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
48
49
50
import 'express-async-errors';
import * as dotenv from 'dotenv';
dotenv.config();
import express from 'express';
import mongoose from 'mongoose';
import morgan from 'morgan';
import devRouter from './routes/devRouter.js';
import userRouter from './routes/userRouter.js';
import statsRouter from './routes/statsRouter.js';
import { errorHandleMiddelware } from './middleware/errorHandleMiddelware.js';
import { authUser } from './middleware/authMiddelware.js';
import cookieParser from 'cookie-parser';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.use(express.static(path.resolve(__dirname, './public')));
app.use(cookieParser());
app.use(express.json());
if (process.env.NODE_ENV) {
app.use(morgan('dev'));
}
app.use('/api/v1/user', userRouter);
app.use('/api/v1/alldevelopers', devRouter);
app.use('/api/v1/stats', authUser, statsRouter);
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, './public', 'index.html'));
});
app.use('*', (req, res) => {
res.status(404).json({ msg: 'Page not found' });
});
app.use(errorHandleMiddelware);
const port = process.env.PORT;
if (!process.env.MONGO_URL) {
console.error("MONGO_URL environment variable is not set.");
process.exit(1);
}
mongoose.connect(process.env.MONGO_URL)
.then(() => {
console.log('Connected to MongoDB');
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
})
.catch(error => {
console.error('Error connecting to MongoDB:', error);
process.exit(1);
});