forked from gitdagray/nodejs_web_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (45 loc) · 1.33 KB
/
server.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
const path = require('path');
const express = require('express')
const app = express()
const PORT = process.env.PORT || 3500
// built-in middleware
app.use(express.urlencoded( { extended: false}))
app.use(express.json())
// serve static files
app.use(express.static(path.join(__dirname, '/public')))
app.get('^/$|/index(.html)?', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'index.html'))
})
app.get('/new-page(.html)? ', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'new-page.html'))
})
app.get('/old-page(.html)?', (req, res) => {
res.redirect(301, '/new-page')
// redirect === app.get('./new-page.html')
})
// Route handler +++, function chain
app.get('/hello(.html)?', (req, res, next) => {
console.log('attempted to load hello.html')
next()
}, (req, res) => {
res.send('HelloWorld!')
})
// chaining route handlers
const one = (req, res, next) =>{
console.log('one')
next()
}
const two = (req, res, next) => {
console.log('two')
// res.send('Finish - 2')
next()
}
const three = (req, res, next) =>{
console.log('three')
res.send('Finish - 3')
}
app.get('/hi', [one, two, three])
app.get('/*', (req, res) => {
res.status(404).sendFile(path.join(__dirname, 'views', '404.html'))
})
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));