我使用快速生成器创建了一个应用程序,一切正常,但我现在想用HTTPS运行应用程序,我试图将/bin/www
中的节点服务器文件配置为以下内容:
#!/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 :(得分:2)
试试这个:
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
答案 1 :(得分:1)
我解决了这个问题,在使用express-generator创建bin / www文件并将其重命名为www后进行修改。然后在package.json中我更改指向新脚本的“source”属性。
try {
FileWriter out = new FileWriter(write_path);
ArrayList<String> file_lines = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader(path));
String line, lineToEncode = "";
while ((line = reader.readLine()) != null) {
file_lines.add(line);
}
for (int i = 0; i < file_lines.size() - 1; i++) {
lineToEncode += file_lines.get(i) + "\n";
}
lineToEncode += file_lines.get(file_lines.size() - 1);
// System.out.print(lineToEncode);
String encodedLine = Base64.getEncoder().encodeToString(lineToEncode.getBytes());
System.out.println(encodedLine);
在www中我使用https服务器而不是http,然后重定向http请求。 这是我的新wwws文件。我删除了www文件中间的http行,最后我添加了新的服务器部分。
...
"scripts": {
"start": "node ./bin/wwws"
},
...
这对我有用,并且让快速生成器创建的结构不受影响。
答案 2 :(得分:0)
使用以下代码在 http 和 https 上进行快速投放。
这正常工作:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var fs = require('fs');
var debug = require('debug')('myapp:server');
var http = require('http');
var https = require('https');
/**
* Get ports from environment and store in Express.
*/
var http_port = normalizePort(process.env.PORT || '80');
var https_port = normalizePort(process.env.HTTPS_PORT || '443');
// app.set('port', port); // comment this line
/**
* Create HTTPS & HTTP servers.
*/
var http_server = http.createServer(app);
var https_server = https.createServer({
key: fs.readFileSync('bin/keys/privateKey.key'),
cert: fs.readFileSync('bin/keys/certificate.crt'),
}, app);
/**
* Listen on provided port, on all network interfaces.
*/
http_server.listen(http_port);
http_server.on('error', onError);
http_server.on('listening', onListening);
https_server.listen(https_port);
https_server.on('error', onErrorHttps);
https_server.on('listening', onListeningHttps);
/**
* 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 & HTTPS servers "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof http_port === 'string'
? 'Pipe ' + http_port
: 'Port ' + http_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;
}
}
function onErrorHttps(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof https_port === 'string'
? 'Pipe ' + https_port
: 'Port ' + https_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 & HTTPS server "listening" event.
*/
function onListening() {
var addr = http_server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
function onListeningHttps() {
var addr = https_server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}