-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
69 lines (60 loc) · 1.54 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
// server.js
// where your node app starts
// init project
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
// we've started you off with Express,
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'))
app.use(bodyParser.json())
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", (request, response) => {
response.sendFile(__dirname + '/views/index.html')
})
// Simple in-memory store
const players = [
{
"name": "foobar",
"x": 100,
"y": 150
},
{
"name": "bizzle",
"x": 200,
"y": 300
}
]
app.get("/players/:id", (request, response) => {
response.setHeader('Content-Type', 'application/json')
response.json(players[request.params.id])
})
app.post("/players/:id", (request, response) => {
console.log(request.body)
var b = request.body
if (b.x && b.y) {
players[request.params.id] = b
}
response.sendStatus(200)
})
app.get("/players/:id/update", (request, response) => {
var p = players[request.params.id]
if (!p) {
players[request.params.id] = {}
p = players[request.params.id]
}
if (request.query.x) {
p.x = request.query.x
}
if (request.query.y) {
p.y = request.query.y
}
if (request.query.name) {
p.name = request.query.name
}
response.sendStatus(200)
})
const listener = app.listen(process.env.PORT, () => {
console.log(`Your app is listening on port ${listener.address().port}`)
})