-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
45 lines (35 loc) · 1.99 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
var express = require("express")
, app = express()
, port = process.env['PORT'] || 3000
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(express.static(__dirname + '/public'))
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><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?</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>")
}
// Routes
app.get("/", home)
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 + '/')