forked from Ahmed-Abdelhafez/Menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (46 loc) · 1.55 KB
/
app.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
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const methodOverride = require('method-override')
const path = require('path')
const ejsMate = require('ejs-mate')
const Restaurant = require('./models/restaurant')
mongoose.connect('mongodb://localhost:27017/Menu',{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
})
mongoose.connection.on('error', console.error.bind(console, 'connection error:'))
mongoose.connection.once('open', () => {
console.log('Database connected')
})
app.engine('ejs', ejsMate)
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.urlencoded({ extended: true }))
app.use(methodOverride('_method'))
app.get('/', (req, res) => {
res.render('home')
})
app.get('/restaurants', async(req, res) => {
const restaurants = await Restaurant.find({})
res.render('restaurants/index', { restaurants })
})
app.get('/restaurants/new', async(req, res) => {
res.render('restaurants/new')
})
app.post('/restaurants', async(req, res) => {
const restaurant = new Restaurant(req.body.restaurant)
await restaurant.save()
res.redirect(`/restaurants/${restaurant._id}`)
})
app.get('/restaurants/:id', async(req, res) => {
const restaurant = await Restaurant.findById(req.params.id)
res.render('restaurants/show', { restaurant })
})
app.post('')
app.listen(3000, () => {
console.log("server is running on port 3000")
})