Skip to content

Commit d132be7

Browse files
committed
Add CORS support
1 parent 07df285 commit d132be7

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ RUN mkdir -p /app && mkdir -p /app/public
44

55
COPY *.js* /app/
66

7-
RUN cd /app && npm install
7+
RUN cd /app && npm ci
88

99
EXPOSE 8080
1010

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Usage
1313

1414
You can expose a local directory which you want to have served via the `mini-webserver` by starting it with
1515

16-
`docker run --name node-web -p 8080:8080 -v /path/to/local/folder:/app/public:ro -d netresearch/node-webserver`
16+
`docker run --name node-web -p 8080:8080 -v $PWD/test:/app/public:ro -d netresearch/node-webserver`
1717

1818
In this example, the port on the docker host where the `node-webserver` is reachable is `8080`.
1919

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
@@ -24,6 +24,7 @@
2424
},
2525
"homepage": "https://hub.docker.com/r/netresearch/node-webserver",
2626
"dependencies": {
27+
"cors": "^2.8.5",
2728
"express": "^4.17.1",
2829
"morgan": "^1.9.1"
2930
}

server.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
const express = require('express'),
22
app = express(),
3-
morgan = require('morgan'),
4-
customize = require('./customize');
3+
cors = require('cors'),
4+
customize = require('./customize'),
5+
morgan = require('morgan')
56

6-
customize(app);
7+
customize(app)
78

8-
app.use(morgan('combined'));
9-
app.use(express.static(__dirname + '/public'));
9+
// Request logger
10+
app.use(morgan('combined'))
1011

12+
// CORS cross origin request policy
13+
app.use(cors({
14+
origin: process.env.CORS || 'https://*.autopilothq.com'
15+
}))
16+
if (process.env.CORS) {
17+
console.info('CORS is set')
18+
}
19+
20+
// Static files
21+
app.use(express.static(__dirname + '/public'))
22+
23+
// Test webserver
1124
app.get('/status', function (req, res) {
12-
res.send('Hello from the Node Webserver!');
25+
res.send('Hello from the Node Webserver!')
1326
});
1427

28+
// Page not found
1529
app.use(function (req, res, next) {
16-
res.status(404).end();
30+
res.status(404).end()
1731
});
1832

19-
app.listen(process.env.PORT || 8080, '0.0.0.0');
33+
app.listen(process.env.PORT || 8080, '0.0.0.0')

tests/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>It works</title>
8+
</head>
9+
<body>
10+
<h1>It works!</h1>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)