diff --git a/server.js b/server.js index 9f0e0f1..79891b0 100644 --- a/server.js +++ b/server.js @@ -123,21 +123,21 @@ if (typeof wo.envi.ssl === 'string') { // else console.log(`Server listening on [${wo.wo.envi.protocol}] http=>https://${wo.wo.envi.host}:${portHttp}=>${portHttps} for ${server.settings.env} environment`) // }) // }else { - require('http') - .createServer( - server.all('*', function (ask, reply) { - reply.redirect(`https://${wo.envi.host}:${portHttps}`) - }) - ) - .listen(portHttp, function (err) { - if (err) console.log(err) - else - console.log( - `[${new Date().toJSON()}] Server redirecting from [${wo.envi.protocol}] http://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${ - wo.envi.prodev - } environment` - ) - }) + require('http') + .createServer(function (ask, reply) { + reply.writeHead(301, { Location: `https://${ask.headers.host.replace(`:${portHttp}`, `:${portHttps}`)}${ask.url}` }) + reply.end() + } + ) + .listen(portHttp, function (err) { + if (err) console.log(err) + else + console.log( + `[${new Date().toJSON()}] Server redirecting from http://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${ + wo.envi.prodev + } environment` + ) + }) webServer = require('https') .createServer( { @@ -151,27 +151,24 @@ if (typeof wo.envi.ssl === 'string') { if (err) console.log(err) else console.log( - `[${new Date().toJSON()}] Server listening on [${wo.envi.protocol}] https://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${ + `[${new Date().toJSON()}] Server listening on https://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${ wo.envi.prodev } environment` ) }) // } } else if ('redirectHttp2Https' === wo.envi.protocol) { - webServer = require('http') - .createServer( - server.all('*', function (ask, reply) { - reply.redirect(`https://${wo.envi.host}`) - }) - // 或者 require('redirect-https')() // https://www.npmjs.com/package/redirect-https - ) - .listen(portHttp, function (err) { - if (err) console.log(err) - else - console.log( - `[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${wo.envi.prodev} environment` - ) - }) + portHttp = 80 + webServer = server.all('*', function (ask, reply) { + reply.redirect(301, `https://${ask.headers.host.replace(`:${portHttp}`, `:${portHttps}`)}${ask.url}`) + }) + .listen(portHttp, function (err) { + if (err) console.log(err) + else + console.log( + `[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${wo.envi.prodev} environment` + ) + }) }else if ('proxyHttps2Http' === wo.envi.protocol) { var proxy=require('http-proxy').createProxyServer({ ssl: {