-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (53 loc) · 1.7 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
const express = require('express')
const dotenv = require('dotenv')
const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
const mongoose = require('mongoose')
const { response } = require('express')
const fs = require('fs')
mongoose.Promise = global.Promise
dotenv.config({ path: './config.env' })
const DB = process.env.DATABASE.replace('<PASSWORD>', process.env.DATABASE_PASSWORD)
app.use(express.static(__dirname + "/public/"))
mongoose.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
}).then(con => {
console.log(con.connections)
console.log('DB connection successful')
})
const nameSchema = new mongoose.Schema({
firstName: String,
lastName: String
})
const User = mongoose.model('User', nameSchema)
app.get("/", (req, res) => {
res.sendFile(`${__dirname}/index.html`)
})
app.get('/addname', (req, res) => {
const response = {
firstName:req.query.firstName,
lastName:req.query.lastName
}
User.find({ firstName: response.firstName, lastName: response.lastName}, "firstName lastName", (err, usernames) => {
if(err) throw err
res.end("hi " + usernames[0].firstName + " " + usernames[0].lastName)
})
})
app.post('/addname', (req, res) => {
const myData = new User(req.body)
myData.save()
.then(item => {
res.send("item saved to database")
})
.catch(err => {
res.status(400).send("unable to save to db")
})
})
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Server is listening on port ${port}...`)
})