Skip to content

Commit

Permalink
ORIGIN env variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
frandiox committed Nov 19, 2017
1 parent f3115e5 commit 48ea557
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ const http = require('http');
const mediumJSONFeed = require('./index.js');

const port = process.env.PORT || 3000;
const respond = (res, data = {}) => {
if (!res.finished) {
res.writeHead(data.status || 500, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
res.end(JSON.stringify(data, null, 2), 'utf-8');
}
const headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': process.env.ORIGIN || '*'
};

http.createServer((req, res) => {
Expand All @@ -20,9 +15,10 @@ http.createServer((req, res) => {

console.log(`> GET: '${req.url}' --- ${new Date()}`);

mediumJSONFeed(req.url)
.then(data => respond(res, data))
.catch(error => respond(res, error));
mediumJSONFeed(req.url, data => {
res.writeHead(data.status || 500, headers);
res.end(JSON.stringify(data, null, 2), 'utf-8');
});

}).listen(port);

Expand Down

0 comments on commit 48ea557

Please sign in to comment.