-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
45 lines (32 loc) · 1.06 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
const express = require('express')
const app = express()
const exphbs = require('express-handlebars')
const mongoose = require('mongoose')
// Include controller
const convertController = require('./controllers/convert')
const homeController = require('./controllers/home')
const port = 3000
// connect mongoose with database
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/url', { useNewUrlParser: true })
const db = mongoose.connection
// error connection
db.on('error', () => {
console.log('mongodb error!')
})
// successful connection
db.once('open', () => {
console.log('mongodb connected!')
})
// Include user model
const Url = require('./models/url')
// set up handlebars
app.engine('handlebars', exphbs({ defaultLayout: 'main' }))
app.set('view engine', 'handlebars')
// serve static files
app.use(express.static('public'))
app.get('/', homeController)
app.get('/convert', convertController.getShortened)
app.get('/:shortenedUrl', convertController.getOriginal)
app.listen(process.env.PORT || port, () => {
console.log('App is running')
})