使 Node js 应用程序 https 安全
Making Node js apps https secure
我正在创建一个链代码项目,其中 nodejs 正在使用 chaincoe 智能合约。
我的项目结构包括 index.js - swagger 规范,app.js - 消费者 swagger 规范和 bin/www - 其中定义了 http 规范。
我已经使用基本身份验证定义了 http,它工作正常。为了使所有服务 https 安全,我在我的 linux 机器上下载了 open ssl 并生成了证书和私钥。 (https://www.linuxhelp.com/how-to-install-and-update-openssl-on-ubuntu-16-04/)
我已经对 https 部分的 bin/www.js 进行了更改:
#!/usr/bin/env node
var app = require('../app');
var fs = require('fs');
var http = require('http');
var https = require('https');
require("dotenv").config();
var privateKey = fs.readFileSync('key.pem').toString();
var certificate = fs.readFileSync('cert.pem').toString();
var port = normalizePort(process.env.PORT || '8080');
app.set('port', port);
var hostname = process.env.HOSTNAME;
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
https.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World!');
res.end();
}).listen(8080);
但这不起作用。我还在 mozilla 中导入了证书和密钥。请求所有人对此提供帮助。
提前致谢。
您需要将 key
和 cert
添加到 createServer
函数中。
const options = {
key: fs.readFileSync('key.pem').toString();
cert: fs.readFileSync('cert.pem').toString();
}
https
.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
})
.listen(443, function(){
console.log("Server listening on localhost:443");
});
现在,正如@aditi 在评论中所说,createServer
中的回调是一个请求处理程序。这意味着它会在有 request
事件时触发。请求事件主要由请求服务器的 HTTP 触发。因此,如果您打开 localhost:443
,它将显示 "hello world"
文本。
如果你想在服务器启动时(列出)控制台记录一些东西,你需要在 listen
函数中添加回调。你做到了。
有效,
我用过
https.createServer(httpsOptions,app)
.listen(port,function(){
console.log("Inside HTTPS creation");
})
谢谢大家
我正在创建一个链代码项目,其中 nodejs 正在使用 chaincoe 智能合约。 我的项目结构包括 index.js - swagger 规范,app.js - 消费者 swagger 规范和 bin/www - 其中定义了 http 规范。 我已经使用基本身份验证定义了 http,它工作正常。为了使所有服务 https 安全,我在我的 linux 机器上下载了 open ssl 并生成了证书和私钥。 (https://www.linuxhelp.com/how-to-install-and-update-openssl-on-ubuntu-16-04/)
我已经对 https 部分的 bin/www.js 进行了更改:
#!/usr/bin/env node
var app = require('../app');
var fs = require('fs');
var http = require('http');
var https = require('https');
require("dotenv").config();
var privateKey = fs.readFileSync('key.pem').toString();
var certificate = fs.readFileSync('cert.pem').toString();
var port = normalizePort(process.env.PORT || '8080');
app.set('port', port);
var hostname = process.env.HOSTNAME;
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
https.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World!');
res.end();
}).listen(8080);
但这不起作用。我还在 mozilla 中导入了证书和密钥。请求所有人对此提供帮助。 提前致谢。
您需要将 key
和 cert
添加到 createServer
函数中。
const options = {
key: fs.readFileSync('key.pem').toString();
cert: fs.readFileSync('cert.pem').toString();
}
https
.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
})
.listen(443, function(){
console.log("Server listening on localhost:443");
});
现在,正如@aditi 在评论中所说,createServer
中的回调是一个请求处理程序。这意味着它会在有 request
事件时触发。请求事件主要由请求服务器的 HTTP 触发。因此,如果您打开 localhost:443
,它将显示 "hello world"
文本。
如果你想在服务器启动时(列出)控制台记录一些东西,你需要在 listen
函数中添加回调。你做到了。
有效,
我用过
https.createServer(httpsOptions,app)
.listen(port,function(){
console.log("Inside HTTPS creation");
})
谢谢大家