将HTTPS服务器从Express迁移到Hapi [英] Migrating HTTPS server from Express to Hapi

查看:70
本文介绍了将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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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