Skip to content

Commit

Permalink
Add CORS support
Browse files Browse the repository at this point in the history
  • Loading branch information
vergissberlin committed Oct 11, 2019
1 parent 07df285 commit d132be7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RUN mkdir -p /app && mkdir -p /app/public

COPY *.js* /app/

RUN cd /app && npm install
RUN cd /app && npm ci

EXPOSE 8080

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Usage

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

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

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

Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"homepage": "https://hub.docker.com/r/netresearch/node-webserver",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"morgan": "^1.9.1"
}
Expand Down
30 changes: 22 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
const express = require('express'),
app = express(),
morgan = require('morgan'),
customize = require('./customize');
cors = require('cors'),
customize = require('./customize'),
morgan = require('morgan')

customize(app);
customize(app)

app.use(morgan('combined'));
app.use(express.static(__dirname + '/public'));
// Request logger
app.use(morgan('combined'))

// CORS cross origin request policy
app.use(cors({
origin: process.env.CORS || 'https://*.autopilothq.com'
}))
if (process.env.CORS) {
console.info('CORS is set')
}

// Static files
app.use(express.static(__dirname + '/public'))

// Test webserver
app.get('/status', function (req, res) {
res.send('Hello from the Node Webserver!');
res.send('Hello from the Node Webserver!')
});

// Page not found
app.use(function (req, res, next) {
res.status(404).end();
res.status(404).end()
});

app.listen(process.env.PORT || 8080, '0.0.0.0');
app.listen(process.env.PORT || 8080, '0.0.0.0')
12 changes: 12 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>It works</title>
</head>
<body>
<h1>It works!</h1>
</body>
</html>

0 comments on commit d132be7

Please sign in to comment.