-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrxjs-server.js
85 lines (82 loc) · 2.4 KB
/
rxjs-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
var http = require("http")
http.createServer(function (req, res) {
// Set CORS headers
res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader("Access-Control-Request-Method", "*")
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT")
res.setHeader("Access-Control-Allow-Headers", "*")
if (req.method === "OPTIONS") {
res.writeHead(200)
res.end()
return
}
switch (req.url) {
case "/endpoint1.json":
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({
data: "data by endpoint1",
id: 1
}))
return
case "/endpoint2/1.json":
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({
data: "data by endpoint2-1"
}))
return
case "/secret1":
if (req.headers.authorization === "abcde") {
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({
data: "data by secret1"
}))
} else {
res.writeHead(401)
res.end()
}
return
case "/secret2":
if (req.headers.authorization === "12345") {
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({
data: "data by secret2"
}))
} else {
res.writeHead(401)
res.end()
}
return
case "/token":
if (req.method === "POST") {
// ログイン
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({
access_token: "abcde",
refresh_token: "12345"
}))
return
} else if (req.method === "PUT") {
// リフレッシュ
if (req.headers.authorization === "12345") {
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({
access_token: "12345",
refresh_token: "abcde"
}))
} else if (req.headers.authorization === "abcde") {
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({
access_token: "abcde",
refresh_token: "12345"
}))
} else {
res.writeHead(401)
res.end()
}
return
}
default:
res.writeHead(404, { "Content-Type": "text/plain" })
res.end("Not Found")
}
}).listen(8887)