-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (31 loc) · 998 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
const express = require('express')
const app = express()
const port = 2005
const cors = require('cors')
const mongoose = require('mongoose')
const nameSchema = require('./schema')
app.use(cors())
app.use(express.json())
// connect to the mongodb database
mongoose.connect('mongodb://mongo:27017/names', {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
}).then(() => console.log("Successfully connected to database"))
.catch(err => console.log("Error connecting to database", err));
// Data model
const Name = mongoose.model('Name', nameSchema)
app.post('/add', (req, res) => {
const { name } = req.body
const name_ = new Name({name});
name_.save()
.then(() => res.send('Successfully added name'))
.catch(err => console.log(err))
})
app.get('/', (req, res) => {
Name.find().then(results => {
res.send(results)
}).catch(err => console.log(err))
})
app.listen(port, () => console.log(`Listening on port ${port}`))