-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (34 loc) · 1.1 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
var database = require('./config/database');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: false }));
// Connect to MongoDB
//mongoose
//.connect(
//'mongodb://mongo:27017/docker-node-mongo',
// { useNewUrlParser: true }
//)
//.then(() => console.log('MongoDB Connected'))
//.catch(err => console.log(err));
mongoose.connect(database.url,
{ useNewUrlParser: true }
)
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));
const Item = require('./models/Item');
app.get('/', (req, res) => {
Item.find()
.then(items => res.render('index', { items }))
.catch(err => res.status(404).json({ msg: 'No items found' }));
});
app.post('/item/add', (req, res) => {
const newItem = new Item({
name: req.body.name
});
newItem.save().then(item => res.redirect('/'));
});
const port = 3000;
app.listen(port, () => console.log('Server running...'));