-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
62 lines (47 loc) · 1.61 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
61
62
require('dotenv').config()
const logger = require('morgan')
const express = require('express')
const errorHandler = require('errorhandler')
const uaParser = require('ua-parser-js')
const port = 3000
const app = express()
const path = require('path')
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(errorHandler())
app.use(express.static(path.join(__dirname, 'public')))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
app.use((req, res, next) => {
// console.log(req.headers)
const ua = uaParser(req.headers['user-agent'])
// console.log(ua)
res.locals.isDesktop = ua.device.type === undefined
res.locals.isPhone = ua.device.type === 'mobile'
res.locals.isTablet = ua.device.type === 'tablet'
next()
})
//=======================All the routes - these can have their own file/folder========================
app.get('/', (req, res) => {
res.render('pages/home')
})
app.get('/about', (req, res) => {
res.render('pages/about')
})
app.get('/threejs', (req, res) => {
res.render('pages/threejs')
})
//=====================================Undefined routes error handling==================
app.all('*', async (req, res, next) => {
res.render('pages/Four04')
})
app.use((err, req, res, next) => {
const { statusCode = 500 } = err;
if (!err.message) err.message = 'Code 500: Something Went Wrong'
res.status(statusCode).send(err.message)
})
//=======================Connecting to port====================================
app.listen(process.env.PORT || port, () => {
console.log(`App listening at http://localhost:${port}`)
})