-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (86 loc) · 2.83 KB
/
index.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
const express = require("express");
const port = 8000;
const bodyParser = require("body-parser");
const multer = require('multer');
const upload = multer({ dest: 'uploads/' })
const router = express.Router();
var app = express();
// parse req body
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use("/", router);
app.post("/token", function (req, res) {
expires_in = 60;
access_token = Date.now() + expires_in * 1000;
if (req.body.grant_type === "client_credentials") {
console.log(
"/token",
"Acquired tokens, will expire on ",
new Date(access_token)
);
} else if (req.body.grant_type === "refresh_token") {
console.log(
"/token",
"Refreshed tokens, will expire on ",
new Date(access_token)
);
} else {
res.sendStatus(400); // bad input
}
res_body = JSON.stringify({
access_token,
refresh_token: "refresh_token",
expires_in,
token_type: "Bearer",
});
console.log("res_body: ", res_body);
res.send(res_body);
});
app.get("/check", function (req, res) {
console.log("/check", "Received request");
console.log("Token: ", req.headers.authorization);
token_split = req.headers.authorization.replace("Bearer ", "");
tokenDate = new Date(parseInt(token_split));
if (Date.now() > tokenDate) {
console.log("Token has expired, returning 401");
res.sendStatus(401);
} else {
console.log("Token accepted");
res.send(JSON.stringify({ status: "ok" }));
}
});
// create multipart form endpoint
app.post('/multipart', upload.single('file'), (req, res) => {
console.log("/multipart headers:" , req.headers);
res.send(JSON.stringify({ req_headers: req.headers }));
});
app.get('/check-headers', (req, res) => {
console.log("/check-headers headers:" , req.headers);
res.send(JSON.stringify({ req_headers: req.headers }));
});
app.get('/test-content-type', function (req, res) {
console.log("/test-content-type headers:", req.headers);
if (req.headers['content-type'] === 'application/json') {
res.sendStatus(400);
} else {
res.send(JSON.stringify({ status: "ok" }));
}
});
app.listen(port, function () {
console.log(`Example app listening on port ${port}!`);
var route,
routes = [];
app._router.stack.forEach(function (middleware) {
if (middleware.route) {
// routes registered directly on the app
routes.push(middleware.route);
} else if (middleware.name === "router") {
// router middleware
middleware.handle.stack.forEach(function (handler) {
route = handler.route;
route && routes.push(route);
});
}
});
console.log(routes);
});