-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (45 loc) · 1.14 KB
/
Copy pathserver.js
File metadata and controls
56 lines (45 loc) · 1.14 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
const express = require('express')
const mongoose = require('mongoose')
const app = new express()
const PORT = 5000
// middleware
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// require model
const Artist = require('./models/Artist')
// require routes
const artistRoute = require('./routes/artistRoute')
const albumRoute = require('./routes/albumRoute')
const mongoURI = 'mongodb://localhost/nestedDemo'
mongoose.connect(mongoURI, { useNewUrlParser: true }, (err) => {
if(err) return console.log(err)
console.log('Successfully connected to MongoDB')
})
// routes configuration
app.use( '/', artistRoute)
app.use( '/', albumRoute)
app.get('/seedArtist', async(req, res) => {
try {
const artist = await Artist.create({
name: "The test",
albums: [
{
title: "tested",
yearReleased: 2019,
songs: [
{
title: 'Anything',
songDuration: 172,
}
]
}
]
})
return res.send(artist)
} catch(err){
return res.send(err)
}
})
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
})