我解決了這個問題與快遞發電機的bin/WWW文件創建後對矯正,並更名爲wwws。然後在package.json中,我更改指向新腳本的「source」屬性。
...
"scripts": {
"start": "node ./bin/wwws"
},
...
在wwws中,我使用https服務器而不是http,然後重定向http請求。 在這裏我的新wwws文件。我刪除了www文件中間的http行,並在最後添加了新的服務器部分。
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('phishsense:server');
var http = require('http');
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
// Add HTTPS Section
var fs = require('fs');
var https = require('https');
var http_port = normalizePort(process.env.PORT || '8080');
var https_port = process.env.PORT_HTTPS || 8443;
var options = {
key : fs.readFileSync('server.key'),
cert : fs.readFileSync('server.crt')
};
app.set("port",https_port);
/*
° Create HTTPS server.
*/
server = https.createServer(options, app).listen(https_port, function() {
console.log('Magic happens on port ' + https_port);
});
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(https_port);
server.on('error', onError);
server.on('listening', onListening);
// Redirect from http port to https
http.createServer(function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'].replace(http_port,https_port) + req.url });
console.log("http requet, will go to >> ");
console.log("https://" + req.headers['host'].replace(http_port,https_port) + req.url);
res.end();
}).listen(http_port);
這對我的作品,並讓快遞發電機不變創建的結構。
的可能的複製[如何創建Node.js的HTTPS服務器?(http://stackoverflow.com/questions/5998694/how-to-create-an-https-server-in-node-js) –
這不是重複的。這個問題是關於將快速自動生成的代碼與https集成。不是簡單地在節點上設置SSL。 –