Skip to content

Commit 8508a86

Browse files
committed
Add CORS support
1 parent 76b76ea commit 8508a86

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const http = require("http")
77
const https = process.env.NODE_ENV === "production"
88
? require("spdy") : require("https")
99
const express = require("express")
10+
const cors = require("cors")
1011
const getCerts = require(path.resolve(__dirname, "certs.js")).getCerts
1112

1213
/* CONFIGURE THE SERVER */
@@ -16,6 +17,9 @@ const createServer = (domain = "localhost") => {
1617
// create a server with express
1718
const app = express()
1819

20+
// add CORS headers to all responses
21+
app.use(cors())
22+
1923
// add getCerts to app
2024
app.getCerts = getCerts
2125

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"dependencies": {
4747
"appdata-path": "^1.0.0",
4848
"compression": "^1.7.4",
49+
"cors": "^2.8.5",
4950
"express": "^4.17.1",
5051
"express-minify": "^1.0.0",
5152
"spdy": "^4.0.2"

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ describe("Testing serve", () => {
199199
})()
200200
})
201201

202+
it("includes access-control-allow-origin header", function(done) {
203+
(async() => {
204+
// set the environment port
205+
process.env.PORT = HTTPS_PORT
206+
// start the server (serving the default folder)
207+
app.serve("test")
208+
// make the request and check the output
209+
await makeRequest("/static.html")
210+
.then(res => assert(res.headers["access-control-allow-origin"] ===
211+
"*"))
212+
done()
213+
})()
214+
})
215+
202216
it("doesn't crash on 404", function(done) {
203217
(async() => {
204218
// set the environment port

0 commit comments

Comments
 (0)