-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (41 loc) · 1.26 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
48
49
const express = require('express')
const cors = require("cors")
const { MongoClient } = require('mongodb')
const userRoutes = require('./routes/userRoutes')
const postRoutes = require('./routes/postRoutes')
const authRoutes = require('./routes/authRoutes')
const app = express()
app.use(express.json())
app.use(cors())
async function onMongoConnect(mongoClient) {
const db = mongoClient.db('saeApp')
const users = db.collection('users')
const posts = db.collection('posts')
// const cursor = await posts
// .aggregate([
// {
// $lookup: {
// from: 'users',
// localField: 'author',
// foreignField: 'username',
// as: 'author',
// },
// },
// ])
// .toArray()
//
// console.log(cursor)
userRoutes(app, { users })
postRoutes(app, { posts })
authRoutes(app, { users })
}
const credentials = { username: 'mongo', password: 'mongo' }
const mongoConnString = `mongodb+srv://${credentials.username}:${credentials.password}@cluster0.kmdytlq.mongodb.net/?retryWrites=true&w=majority`
new MongoClient(mongoConnString)
.connect()
.catch(console.error)
.then(onMongoConnect)
app.listen(3000, () => {
// eslint-disable-next-line no-console
console.log('Server startet on Port 3000')
})