NodeJS http 和 https 模块有什么区别?

问题描述 投票:0回答:4

我在我的项目中使用http模块,但我的大多数“post”请求被邮递员阻止。 我读到这是一个 ssl 问题,经过一番研究,我发现了另一个名为 https 的模块。

这是我当前的代码。

 var http = require('http');

var server = http.createServer(app);
node.js web-services rest http ssl-certificate
4个回答
4
投票

嘿,请确保 Postman 中的拦截器已关闭(它应该位于顶部,“登录”按钮左侧)

与 https 相关,如 Node.js v5.10.1 文档中所述

HTTPS 是基于 TLS/SSL 的 HTTP 协议。在 Node.js 中,这是作为单独的模块实现的。

我曾经用它通过 https(端口 443)从我的服务器向其他服务器发出请求。

顺便说一句,你的代码不应该工作,试试这个

const http = require('http');
http.createServer( (request, response) => {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');

并在 Postman 中使用 http://127.0.0.1:8124 ..希望有帮助


1
投票
const http = require('http');
http.createServer( function(request, response) {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://localhost:3000/');

1
投票

HTTP
HTTPS
之间的区别是,如果您需要通过
SSL
与服务器通信,使用证书加密通信,则应使用
HTTPS
,否则应使用
HTTP
,例如:

使用

HTTPS
你可以做类似的事情:

var https = require('https');

var options = {
    key : fs.readFileSync(path.resolve(__dirname, '..', 'ssl', 'prd', 'cert.key')).toString(),
    cert : fs.readFileSync(path.resolve(__dirname, '..', 'ssl', 'prd', 'cert.crt')).toString(),
};

var server = https.createServer(options, app);
server = server.listen(443, function() {
    console.log("Listening " + server.address().port);
});

0
投票

使用 HTTP 交换的超文本作为纯文本,即如果拦截此数据交换,浏览器和服务器之间的任何人都可以相对轻松地读取它,因此它是不安全的,而 HTTPS 被认为是安全的,但以处理成本为代价因为 Web 服务器和 Web 浏览器需要使用证书交换加密密钥,然后才能传输实际数据。 HTTP 工作在应用层,但 HTTPS 工作在传输层,这使得 HTTPS 更安全、更加密,但比 HTTP 慢一点。

© www.soinside.com 2019 - 2024. All rights reserved.