Node.js IPv6问题 [英] Node.js IPv6 issue

查看:360
本文介绍了Node.js IPv6问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js服务器已部署在Google Cloud Platform上,并且该服务器未在ipv6上进行侦听. 当我在移动浏览器(chrome)中运行我的API时,看到此错误:该网站无法提供安全的连接. ERR_SSL_PROTOCOL_ERROR

Node.js server is deployed on Google Cloud Platform and the server is not listening on ipv6. When I run my API in a mobile browser(chrome) I am seeing this error: This site can't provide a secure connection. ERR_SSL_PROTOCOL_ERROR

如何使node.js服务器在ipv6上运行? 以下是我的代码:

How to make node.js server run on ipv6? Following is my code:

const app = express();
const port = 8080;

const privateKey = fs.readFileSync(
  '/path/to/privkey.pem',
  'utf8'
);
const certificate = fs.readFileSync(
  '/path/to/cert.pem',
  'utf8'
);
const ca = fs.readFileSync(
  '/path/to/chain.pem',
  'utf8'
);

const options = {
  key: privateKey,
  cert: certificate,
  ca: ca
};

https.createServer(options, app).listen(port, '::', () => {
    console.log(`Server started on port ${port}`);
  });

推荐答案

我建议您尝试这段代码.您应该能够在本地主机的8082端口中对其进行检查: https://localhost:8082/

I recommend you to try this piece of code. You should be able to check it in 8082's port in localhost: https://localhost:8082/

const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8082);

希望这会有所帮助.

这篇关于Node.js IPv6问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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