NodeJs HTTPS连接与快速生成器
NodeJs HTTPS connection with express generator
我已经使用快速生成器创建了一个应用程序,它一切正常,但我现在想使用 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://时,我无法再访问它
试试这个:
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
我解决了这个问题,在创建后使用快速生成器修改 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节点mongodb中的连接和查询队列
- 如何通过安全的https连接在javascript中使用基于soap xml的Web服务
- CasperJS(phantom 1.9.7),从昨天起就无法连接https
- HTTP 或 HTTPS 根据当前连接
- 如何通过ssl、https Node.js进行连接
- 测试客户端'通过https-ASP.net连接的能力
- 在SEEM Beacon Manager iOS Cordova SDK中设置HTTPs连接
- NodeJs HTTPS连接与快速生成器
- 确定 https 连接中使用的密码
- Node Js不会通过http连接,但会通过https连接
- 如何通过https连接使用google.load加载谷歌库
- https连接中加载的Http内容
- 如何使用node.js通过https实现mongodb连接
- JavaScript按钮不能与HTTPS连接一起工作
- 使用wss://时,Websockets和https不会连接
- Google Earth api允许使用未注册证书进行https连接
- HTTPS 网站中的网络套接字连接错误
- 不能用React Native连接Socket io到https