-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (33 loc) · 957 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
41
42
43
import express from 'express';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import postRoutes from './routes/posts.js';
import userRoutes from './routes/users.js';
import cors from 'cors';
dotenv.config();
const port=process.env.PORT;
const app= express();
app.use(express.json());
app.use(cors());
app.use((req, res, next) => {
console.log(req.path, req.method);
next();
});
app.use('/api/posts', postRoutes);
app.use('/api/user', userRoutes);
const connectDB = async()=> {
try{
mongoose.set('strictQuery',true);
mongoose.connect(process.env.MONGO_URI,{
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log('MongoDB connected');
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
connectDB().then(() => {
app.listen(port, () => console.log(`listening on port ${port}`));
}).catch(err => console.log(err));