2016-03-15 91 views
1

我用快遞發電機,所有的作品不錯,但我現在要運行使用HTTPS應用程序創建一個應用程序有明確的發電機連接,我試圖將節點配置服務器文件中/bin/www以下幾點:HTTPS的NodeJS

#!/usr/bin/env node 

/** 
* Module dependencies. 
*/ 

var debug = require('debug')('****:server'); 
var https = require('https'); 
var fs = require('fs'); 

/** 
* Get port from environment and store in Express. 
*/ 

var port = normalizePort(process.env.PORT || '3001'); 
app.set('port', port); 


var options = { 
    path: '../app', 
    port: 443, 
    key: fs.readFileSync('/var/www/vhosts/keys/wildcard.****.com.key'), 
    cert: fs.readFileSync('/var/www/vhosts/keys/wildcard.****.com.crt') 
} 


/** 
* Create HTTPS server. 
*/ 

var server = https.createServer(options); 

,但我可以指向HTTPS時不再訪問我的應用程序://

+0

的可能的複製[如何創建Node.js的HTTPS服務器?(http://stackoverflow.com/questions/5998694/how-to-create-an-https-server-in-node-js) –

+0

這不是重複的。這個問題是關於將快速自動生成的代碼與https集成。不是簡單地在節點上設置SSL。 –

回答

1

嘗試了這一點:

var express = require('express'); 
 
var https = require('https'); 
 
var http = require('http'); 
 
var fs = require('fs'); 
 

 
// This line is from the Node.js HTTPS documentation. 
 
var options = { 
 
    key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), 
 
    cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert') 
 
}; 
 

 
// Create a service (the app object is just a callback). 
 
var app = express(); 
 

 
// Create an HTTP service. 
 
http.createServer(app).listen(80); 
 
// Create an HTTPS service identical to the HTTP service. 
 
https.createServer(options, app).listen(443);

,並創建一個自簽名的證書。怎麼樣 ?請點擊此鏈接http://docs.nodejitsu.com/articles/HTTP/servers/how-to-create-a-HTTPS-server

0

我解決了這個問題與快遞發電機的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); 

這對我的作品,並讓快遞發電機不變創建的結構。