-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathserver.js
67 lines (56 loc) · 1.71 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
const express = require("express");
const cors = require("cors");
const app = express();
/* -------------------------------------------------------------------------- */
app.get("/no-cors", (req, res) => {
console.info("GET /no-cors");
res.json({
text: "You should not see this via a CORS request."
});
});
/* -------------------------------------------------------------------------- */
app.head("/simple-cors", cors(), (req, res) => {
console.info("HEAD /simple-cors");
res.sendStatus(204);
});
app.get("/simple-cors", cors(), (req, res) => {
console.info("GET /simple-cors");
res.json({
text: "Simple CORS requests are working. [GET]"
});
});
app.post("/simple-cors", cors(), (req, res) => {
console.info("POST /simple-cors");
res.json({
text: "Simple CORS requests are working. [POST]"
});
});
/* -------------------------------------------------------------------------- */
app.options("/complex-cors", cors());
app.delete("/complex-cors", cors(), (req, res) => {
console.info("DELETE /complex-cors");
res.json({
text: "Complex CORS requests are working. [DELETE]"
});
});
/* -------------------------------------------------------------------------- */
const issue2options = {
origin: true,
methods: ["POST"],
credentials: true,
maxAge: 3600
};
app.options("/issue-2", cors(issue2options));
app.post("/issue-2", cors(issue2options), (req, res) => {
console.info("POST /issue-2");
res.json({
text: "Issue #2 is fixed."
});
});
/* -------------------------------------------------------------------------- */
if (!module.parent) {
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log("Express server listening on port " + port + ".");
});
}