gulp-livereload over https?



我一直在使用插入 http://的 livereload chrome 扩展程序 [...]/livereload.js 到文档中。不幸的是,我正在研究一个需要https的项目,我希望在本地复制它,但我没有必要这样做,因为我可以更改不同环境的协议,但我想知道是否可以将gulp-livereload设置为通过https加载?

我尝试了几件事,例如手动添加脚本但没有成功,因为我收到连接错误(GET https://127.0.0.1:35729/livereload.js?snipver=1 net::ERR_CONNECTION_CLOSED):

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://127.0.0.1:35729/livereload.js?snipver=1';
document.getElementsByTagName('body')[0].appendChild(script)

我有两个解决方案:

1. 自己托管客户

https://github.com/livereload/livereload-js

将其安装为依赖项:

bower install livereload-js --save-dev

npm install livereload-js --save

然后只需包含它,就像包含常规脚本一样。但是请注意这些注意事项。

/path/to/dist/livereload.js?host=localhost

2. 代理客户端脚本

这很复杂,但它肯定会起作用。在您的 gulpfile 中创建一个 HTTPS 服务器(可能在监视任务中)。

https://github.com/nodejitsu/node-http-proxy

https://github.com/nodejitsu/node-http-proxy#using-https

httpProxy.createServer({
  ssl: {
    key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
    cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
  },
  target: 'https://localhost:35729',
  secure: true // Depends on your needs, could be false.
}).listen(443);

然后,您可以引用代理脚本。

https://127.0.0.1:443/livereload.js?snipver=1

虽然不在 API 文档中,但添加密钥/证书参数有效

gulp.task('run-reload-server', function() {
  livereload.listen({
    host: "my.domain.com",
    port: 35729,
    key: fs.readFileSync(path.join(__dirname, 'livereload.key'), 'utf-8'),
    cert: fs.readFileSync(path.join(__dirname, 'livereload.crt'), 'utf-8'),
  });
});

最新更新