Node.js IPv6 issue

喜夏-厌秋 提交于 2019-12-11 15:57:15

问题


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

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}`);
  });

回答1:


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);

Hope this helps.



来源:https://stackoverflow.com/questions/59136690/node-js-ipv6-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!