-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (30 loc) · 1.03 KB
/
index.js
File metadata and controls
44 lines (30 loc) · 1.03 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: false }));
// Connect to MongoDB
mongoose.connect('mongodb://mongo:27017/app',
// mongo config when working with docker
{ useNewUrlParser: true }
)
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));
const Item = require('./models/Item');
app.get('/', (req, res) => {
console.log("hit home route");
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,
post: req.body.post
});
newItem.save().then(item => res.redirect('/'));
});
const port = 3000
// which port is the node server listening on?
app.listen(port, () => console.log('I will not run baby!', port));