nodejs https服务器无响应 [英] nodejs https server doesn't respond

查看:361
本文介绍了nodejs https服务器无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的https服务器正在运行,但无法处理请求:

const crypto = require('crypto'),
      fs = require("fs"),
      http = require("http");
var https = require('https');

var privateKey = fs.readFileSync('C:\\dev\\apps\\OpenSSL-Win64\\batar\\azerty.pem');
var cert = fs.readFileSync('C:\\dev\\apps\\OpenSSL-Win64\\batar\\tg.pem');


var options = {
  key: privateKey,
  cert: cert
};

console.log( options );

https.createServer(options, function (req, res) {
  console.log("handling request");
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8000);

当我输入服务器地址localhost:8000时,浏览器将加载一段时间,最终会出现空响应"错误.

这是服务器输出:

C:\>node dev\nouille\server.js
{ key: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 52 53 41 20 50 52 49 56 41 54 45 20 4b 45 59 2d 2d 2d 2d 2d 0a 4d 49 49 43 58 51 4
9 42 41 41 4b 42 67 51 44 56 61 4c 41 ...>,
  cert: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0a 4d 49 49 43 66 7a 43 43 41 65
67 43 43 51 43 63 66 52 6a 78 57 32 72 ...> }

解决方案

几乎可以肯定这是由于未通过https:协议进行连接而引起的.在大多数浏览器中,从URL中删除"http(s://://")时,这是一个容易犯的错误,尤其是在将开发者服务器配置在https://localhost:8443或其他非标准端口上的开发箱中. /p>

因此,在拉出更多头发之前,请先检查协议.

-感谢 @ user568109 提供的指导!

My https server is running but it doesn't handle requests:

const crypto = require('crypto'),
      fs = require("fs"),
      http = require("http");
var https = require('https');

var privateKey = fs.readFileSync('C:\\dev\\apps\\OpenSSL-Win64\\batar\\azerty.pem');
var cert = fs.readFileSync('C:\\dev\\apps\\OpenSSL-Win64\\batar\\tg.pem');


var options = {
  key: privateKey,
  cert: cert
};

console.log( options );

https.createServer(options, function (req, res) {
  console.log("handling request");
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8000);

When I enter the server address localhost:8000, the browser loads for ages and will end up with an "empty response" error.

Here is the server output:

C:\>node dev\nouille\server.js
{ key: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 52 53 41 20 50 52 49 56 41 54 45 20 4b 45 59 2d 2d 2d 2d 2d 0a 4d 49 49 43 58 51 4
9 42 41 41 4b 42 67 51 44 56 61 4c 41 ...>,
  cert: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0a 4d 49 49 43 66 7a 43 43 41 65
67 43 43 51 43 63 66 52 6a 78 57 32 72 ...> }

解决方案

This is almost certainly caused by not connecting over the https: protocol. With most browsers eliminating the "http(s)://" from the url, it's an easy mistake to make, especially on a dev box where the https server is configured on https://localhost:8443, or some other non-standard port.

So, check your protocol first before pulling out any more of your hair.

-- Thanks @user568109 for the guidance!

这篇关于nodejs https服务器无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆