-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (32 loc) · 877 Bytes
/
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
import express from 'express';
import { readdirSync } from 'fs';
import cors from 'cors';
import dotenv from 'dotenv';
import mongoose from 'mongoose';
const logger = require('morgan');
// Create express app
const app = express();
dotenv.config();
mongoose
.connect(process.env.DATABASE)
.then(() => console.log('DB Connected'))
.catch((e) => console.error(e));
// Apply Middlewares
app.use(logger('dev'));
app.use(express.json());
app.use(
cors({
origin: [
'http://localhost:3000',
'https://learnlit.vercel.app',
'https://learnlit.tech',
],
credentials: true,
})
);
app.get('/', (req, res) => {
res.send('API available at /api');
});
readdirSync('./routes').map((r) => app.use('/api', require(`./routes/${r}`)));
const PORT = process.env.PORT || 8000;
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));