将HTTPS服务器从Express迁移到Hapi

编程入门 行业动态 更新时间:2024-10-24 20:21:14
本文介绍了将HTTPS服务器从Express迁移到Hapi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将HTTPS服务器从Express迁移到Hapi.服务器在Express上运行良好,但是当我尝试在Hapi中运行服务器时,我收到消息,提示无效的服务器选项"和不允许TLS".

I'm trying to migrate an HTTPS server from Express to Hapi. The server is running fine on Express, but when I try to run it in Hapi I get messages saying "Invalid server options" and "TLS is not allowed".

这是我使用Express的(简体)代码:

This is my (simplified) code with Express:

var fs = require('fs'); var https = require('https'); var app = require('express')(); var options = { key: fs.readFileSync('server.key'), cert: fs.readFileSync('server.crt') }; app.get('/', function (req, res) { res.send('Hello World!'); }); https.createServer(options, app).listen(8081);

这是我的Hapi(简化)代码:

And this is my (simplified) code with Hapi:

var fs = require('fs'); var Hapi = require('hapi'); var options = { tls: { key: fs.readFileSync('server.key'), cert: fs.readFileSync('server.crt') } }; var server = new Hapi.Server(options); server.connection({ host: 'localhost', port: 8081 }); server.route({ method: 'GET', path: '/', handler: function (request, reply) { return reply('Hello world!'); } }); server.start();

我使用的是自签名证书,但我想应该没问题吗?它可以在Express中使用.

I'm using a self-signed certificate, but I guess that should be fine? It does work in Express.

推荐答案

您的代码看起来非常接近.我相信,要使Hapi使用证书,您要做的所有工作就是将其移至server.connection调用,例如:

Your code looks pretty close. I believe all you have to do to get Hapi to use your certificate and key is to just move it over to the server.connection call, such as:

server.connection({ host: 'localhost', port: 8081, tls: { key: fs.readFileSync('server.key'), cert: fs.readFileSync('server.crt') } });

更多推荐

将HTTPS服务器从Express迁移到Hapi

本文发布于:2023-10-31 15:35:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   HTTPS   Hapi   Express

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!