-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
100 lines (83 loc) · 3.53 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var express = require("express")
, app = express()
, port = process.env['PORT'] || 3000
// Route handlers
function home(req,res) {
res.setHeader("Content-Type", 'text/html')
res.send('<html><head><title>NodeSlash</title></head><body><h1>Web\'s Premiere Browser Game</h1><p><form action="/login" method="POST"><label for="username">Username</label><input id="username" type="text" name="username"><button type="submit">Start ye adventures!</button></form></p><p><img src="/images/sword_and_shield.png"></p><a href="/adventures">Go to adventures</a></body></html>')
}
function adventuresIndex(req,res) {
res.setHeader("Content-Type", 'text/html')
res.send("<html><head><title>Adventures - NodeSlash</title></head><body><h1>Are ye bravez, " + req.session.username + "?</h1><form action='/adventures' method='POST'><button type='submit'>Yes I am bravez</button></form></body></html>")
}
function createAdventure(req,res) {
res.setHeader("Content-Type", 'text/html')
res.send('<html><head><title>Adventures - NodeSlash</title></head><body><h1>Thou hast bravez.</h1><form action="/adventures/42" method="POST"><input type="hidden" name="_method" value="put"><button type="submit">Cheat!</button></form><form action="adventures" method="POST"><button type="submit">Again!</button></form><p>You have found some <a href="/loot/1">loot.</a></p></body></html>')
}
function updateAdventure(req,res) {
res.setHeader("Content-Type", 'text/html')
res.send("<html><head><title>Adventures - NodeSlash</title></head><body><h1>It's a secret to everybody.</h1></body></html>")
}
function showLoot(req,res) {
var id = req.params.id
res.setHeader("Content-Type", 'text/html')
res.send("<html><head><title>Adventures - NodeSlash</title></head><body><h1>Ogre-slaying knife</h1><p>It has +9 against ogres. It was id #" + id + "</p></body></html>")
}
function login(req,res) {
if (req.body.username) {
req.session.username = req.body.username
res.redirect("/adventures")
} else {
res.redirect("/")
}
}
// Middlwarez
function stoopidLogger(options) {
return function stoopidLoggerInner(req,res,next) {
console.log("Hi! I was called at: ", req.path)
next()
}
}
function ensureAuthenticated(req,res,next) {
if (req.session.username) {
next()
} else {
res.redirect("/")
}
}
function notFound(req,res) {
res.setHeader("Content-Type", 'text/html')
res.send(404, "Confound it all! We could not find ye's page! ")
}
function catchErrors(err,req,res,next) {
console.log("There was an error! In the real world, I might be alerting the engineering team.")
next(err)
}
function showErrorPage(err,req,res,next) {
res.setHeader("Content-Type", 'text/html')
res.send(500, "Confound it all! It's a 500 error!")
}
function readFromDB(req,res,next) {
next(new Error("We don't need no stinkin' DB!"))
}
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(express.cookieParser())
app.use(express.cookieSession({secret: "it'sasecrettoeverybody", key: "session"}))
app.use("/", stoopidLogger())
app.use(express.static(__dirname + '/public'))
app.use(app.router)
app.use(notFound)
app.use(catchErrors)
app.use(showErrorPage)
// Routes
app.get("/", home)
app.get("/read_from_db", readFromDB)
app.post("/login", login)
app.all("/*", ensureAuthenticated)
app.get("/adventures", adventuresIndex)
app.post("/adventures", createAdventure)
app.put("/adventures/:id", updateAdventure)
app.get("/loot/:id", showLoot)
app.listen(port)
console.log('Server running at http://127.0.0.1:' + port + '/')