javascript Api网关nodejs多端口http https
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Api网关nodejs多端口http https相关的知识,希望对你有一定的参考价值。
const express = require('express');
const proxy = require('http-proxy-middleware');
const fs = require("fs");
const { createServer } = require('https');
const certOptions = {
key: fs.readFileSync('/etc/letsencrypt/live/s1.example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/s1.example.com/fullchain.pem'),
};
const mainSystem = express();
// support https
createServer(certOptions, mainSystem).listen(443, (err) => {
if (err) throw err;
console.log('> Ready on https://example.com');
});
// redirect to https
mainSystem.use(function(req, res, next) {
if (req.secure) {
// request was via https, so do no special handling
next();
}
else {
// request was via http, so redirect to https
// res.redirect('https://' + req.headers.host + req.url);
}
});
mainSystem.use('*', (req, res, next) => {
switch (req.headers.host) {
case 'example.com': // like nextjs
proxy({ target: 'http://localhost:portweb', changeOrigin: true })(req, res, next);
break;
case 's1.example.com': // backend microservice 01, with websocket
proxy({ target: 'http://localhost:port01', changeOrigin: true, ws: true })(req, res, next);
break;
case 's2.dev.houselink.info': // backend microservice 01, with websocket
proxy({ target: 'http://localhost:port02', changeOrigin: true, ws: true })(req, res, next);
break;
default:
proxy({ target: 'http://localhost:portweb', changeOrigin: true })(req, res, next);
}
});
mainSystem.listen(80);
以上是关于javascript Api网关nodejs多端口http https的主要内容,如果未能解决你的问题,请参考以下文章
使用 nodeJs 构建网关
NodeJS 应用程序无法从 Zuul API 网关访问
使用 AWS api 网关 + lambda + Nodejs 的私有和公共 ip
API网关Kong使用指南(六)—— 支持HTTPS
Apache多目录端口
使用 loopback4 创建 API 网关