如何创建 hapi http
和 https
服务器,同时监听 80 和 443 并使用相同的路由?
(我需要一个服务器,它应该使用完全相同的 API 在 http 和 https 上运行)
最佳答案
直接在应用程序上处理 https 请求可能并不常见,但 Hapi.js 可以在同一个 API 中同时处理 http 和 https。
var Hapi = require('hapi');
var server = new Hapi.Server();
var fs = require('fs');
var tls = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem')
};
server.connection({address: '0.0.0.0', port: 443, tls: tls });
server.connection({address: '0.0.0.0', port: 80 });
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('Hello, world!');
}
});
server.start(function () {
console.log('Server running');
});
关于node.js - HAPI JS Node js 创建 https 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27751741/