Commit 255d4694 authored by Mikey's avatar Mikey
Browse files

change server to only redirect

parent c6876605
......@@ -22,9 +22,8 @@
},
"homepage": "https://github.com/valueflows/valueflo.ws#readme",
"dependencies": {
"finalhandler": "^0.4.0",
"http-cors": "^1.0.1",
"serve-static": "^1.10.0"
"redirekt": "^1.1.0"
},
"devDependencies": {
"git-directory-deploy": "^1.3.0",
......
var finalhandler = require('finalhandler')
var http = require('http')
var https = require('https')
var serveStatic = require('serve-static')
var Cors = require('http-cors')
var Url = require('url')
var redirect = require('redirekt')
var home = 'https://www.valueflo.ws'
var ghPages = {
'/linked-data-browser': 'https://valueflows.github.io/linked-data-browser'
}
var cors = new Cors()
var serve = serveStatic(__dirname)
var server = http.createServer(function(req, res){
var done = finalhandler(req, res)
if (cors.apply(req, res))
return; // this was an OPTIONS request - no further action needed
for (var pageUrl in ghPages) {
var pageUrlIndex = req.url.indexOf(pageUrl)
if (pageUrlIndex === 0) {
var ghPagesUrl = ghPages[pageUrl] + req.url.slice(pageUrl.length)
return proxy(ghPagesUrl)(req, res, done)
return redirect(req, res, ghPagesUrl)
}
}
serve(req, res, done)
redirect(req, res, home)
})
server.listen(process.env.PORT || 3000)
function proxy (to) {
return function (req, res, done) {
var urlObj = Url.parse(to)
var options = {
protocol: urlObj.protocol,
hostname: urlObj.hostname,
port: urlObj.port,
method: req.method,
path: urlObj.path,
headers: req.headers
}
options.headers.host = options.hostname
var request = (urlObj.protocol === 'https:' ? https : http).request
request(options, function (pes) {
res.writeHead(pes.statusCode, pes.headers)
pes.pipe(res)
})
.on('error', done)
.end()
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment