搭建微服务器:express+https+api代理
概述
最近打算玩一下service worker,但是service worker只能在https下跑,所以查资料自己用纯express搭建了一个微服务器,把过程记录下来,供以后开发时参考,相信对其他人也有用。
参考资料:express官方文档
http服务器
首先我们用express搭建一个http服务器,很简单,看看官方文档就可以搭建出来了。代码如下:
// server.js
const express = require('express');
const http = require('http');
const app = express();
const PORT = 7088; // 写个合理的值就好
const httpServer = http.createServer(app);
app.get('/', function (req, res) {
res.send('hello world');
});
httpServer.listen(PORT, function () {
console.log('HTTPS Server is running on: http://localhost:%s', PORT);
});
加入到项目中
我们的理想状况是,在项目目录下建立一个server文件夹,然后在server文件夹里面启动服务器,加载项目目录下的dist文件夹。
所以我们加入代码解析静态资源:
// server.js
const express = require('express');
const http = require('http');
const app = express();
const PORT = 7088; // 写个合理的值就好
const httpServer = http.createServer(app);
app.use('/', express.static('../dist'));
httpServer.listen(PORT, function () {
console.log('HTTPS Server is running on: http://localhost:%s', PORT);
});
加入https
我们想把http变成https,首先我们要生成本地证书:
brew install mkcert
mkcert localhost 127.0.0.1 ::1
上面的代码意思是说,先安装mkcert,然后用mkcert给localhost,127.0.0.1和::1这三个域名生成证书。
然后我们可以在文件夹下面看到2个文件:
秘钥:example.com+3-key.pem
公钥:example.com+3.pem
我们在钥匙串里面把公钥添加信任。方法可参考:在Vue里用Service Worker来搞个中间层(React同理)
添加完之后我们把秘钥和公钥放在certificate文件夹,然后添加到credentials.js文件中,我们通过这个文件引入秘钥和公钥:
// credentials.js
const path = require('path');
const fs = require('fs');
// 引入秘钥
const privateKey = fs.readFileSync(path.resolve(__dirname, './certificate/example.com+3-key.pem'), 'utf8');
// 引入公钥
const certificate = fs.readFileSync(path.resolve(__dirname, './certificate/example.com+3.pem'), 'utf8');
module.exports = {
key: privateKey,
cert: certificate
};
最后我们把http变成https,并且引入秘钥和公钥:
// server.js
const express = require('express');
const https = require('https');
const credentials = require('./credentials');
const app = express();
const SSLPORT = 7081; // 写个合理的值就好
const httpsServer = https.createServer(credentials, app);
app.use('/', express.static('../dist'));
httpsServer.listen(SSLPORT, function () {
console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT);
});
设置api代理
在项目中,我们经常遇到跨域问题,在开发时我们是通过devServer的proxyTable解决的,而proxyTable在打包后是无效的。所以我们需要在服务器上面代理api请求。代码如下:
// proxy.js
const proxy = require('http-proxy-middleware');
const authApi = 'your-authApi-address';
const commonApi = 'your-commonApi-address';
module.exports = app => {
app.use('/api/auth', proxy({
target: authApi,
changeOrigin: true,
pathRewrite: {
'/api/auth': '/auth'
},
secure: false,
}));
app.use('/api/common', proxy({
target: commonApi,
changeOrigin: true,
pathRewrite: {
'/api/common': '/api'
},
secure: false,
}));
};
写法和devServer里面是一样的,因为devServer底层也是通过express实现的。
然后我们在server.js里面引入上面写的代理:
// server.js
const express = require('express');
const https = require('https');
const setProxy = require('./proxy');
const credentials = require('./credentials');
const app = express();
const SSLPORT = 7081; // 写个合理的值就好
const httpsServer = https.createServer(credentials, app);
app.use('/', express.static('../dist'));
setProxy(app);
httpsServer.listen(SSLPORT, function () {
console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT);
});
最后
最后我们把server.js,credentials.js和proxy.js放在一起就好了啦!
用法:只需要把整个文件夹放到项目目录,在里面运行下面的指令就好了:
yarn i
node server.js
详细代码可以参考我的github
搭建微服务器:express+https+api代理的更多相关文章
- spring cloud+dotnet core搭建微服务架构:Api授权认证(六)
前言 这篇文章拖太久了,因为最近实在太忙了,加上这篇文章也非常长,所以花了不少时间,给大家说句抱歉.好,进入正题.目前的项目基本都是前后端分离了,前端分Web,Ios,Android...,后端也基本 ...
- spring cloud+.net core搭建微服务架构:Api授权认证(六)
前言 这篇文章拖太久了,因为最近实在太忙了,加上这篇文章也非常长,所以花了不少时间,给大家说句抱歉.好,进入正题.目前的项目基本都是前后端分离了,前端分Web,Ios,Android...,后端也基本 ...
- spring cloud+dotnet core搭建微服务架构:Api网关(三)
前言 国庆假期,一直没有时间更新. 根据群里面的同学的提问,强烈推荐大家先熟悉下spring cloud.文章下面有纯洁大神的spring cloud系列. 上一章最后说了,因为服务是不对外暴露的,所 ...
- spring cloud+.net core搭建微服务架构:Api网关(三)
前言 国庆假期,一直没有时间更新. 根据群里面的同学的提问,强烈推荐大家先熟悉下spring cloud.文章下面有纯洁大神的spring cloud系列. 上一章最后说了,因为服务是不对外暴露的,所 ...
- 微信小程序语音识别服务搭建全过程解析(https api开放,支持新接口mp3录音、老接口silk录音)
silk v3(或新录音接口mp3)录音转olami语音识别和语义处理的api服务(ubuntu16.04服务器上实现) 重要的写在前面 重要事项一: 所有相关更新,我优先更新到我个人博客中,其它地方 ...
- 搭建nginx服务器,虚拟主机,反向代理
1 搭建Nginx服务器 1.1 问题 在IP地址为192.168.10. 5的主机上安装部署Nginx服务,并可以将Nginx服务器升级到更高版本,要求编译时启用如下功能: SSL加密功能 查看服务 ...
- 搭建gerrit服务器(apache&nginx反向代理方式)
这段时间,想搭建一个gerrit,用于代码托管,gerrit的搭建,网上有很多种教程,但是自己按照别人的教程逐步操作,一直出现诸多问题.最头痛的就是:Configuration Error Check ...
- CentOS搭建svn服务器支持https访问
在CentOS6.3 64位机器上配置SVN服务器,并设置只允许HTTPS连接,可以配置多个repos源,每个源都拥有自己的组和成员,用于权限控制. 安装相关软件 Apache yum install ...
- spring cloud+dotnet core搭建微服务架构:配置中心续(五)
前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...
随机推荐
- 进击Node.js基础(二)promise
一.Promise—Promise似乎是ES6中的规范 PROMISE的语言标准,PROMISE/A+规范,如何使用,在什么场景下使用 Promise时JS对异步操作场景提出的解决方案(回调,观察者模 ...
- json & pickle 序列化
#!/usr/bin/python # -*- coding: utf-8 -*- # 序列化: 例如把字典写进文件 info = { 'name': 'alex', 'age': 22 } f = ...
- Goroutine陷阱
Go在语言层面通过Goroutine与channel来支持并发编程,使并发编程看似变得异常简单,但通过最近一段时间的编码,越来越觉得简单的东西,很容易会被滥用.Java的标准库也让多线程编程变得简单, ...
- [ZLXOI2015]殉国 数论 扩展欧几里得
题目大意:已知a,b,c,求满足ax+by=c (x>=0,y>=0)的(x+y)最大值与最小值与解的个数. 直接exgcd,求出x,y分别为最小正整数的解,然后一算就出来啦 #inclu ...
- UOJ_274_[清华集训2016]温暖会指引我们前行_LCT
UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
- nginx + tomcat 反向代理
简单的配置:# my test java+nginx project server { listen ; server_name localhost; root /home/user/Desktop/ ...
- 如何运行vue项目
首先,列出来我们需要的东西: node.js环境(npm包管理器) vue-cli 脚手架构建工具 cnpm npm的淘宝镜像 安装node.js 从node.js官网下载并安装node,安 ...
- linux系统光盘开机自动挂载-配置本地yum源
一.光盘开机自动挂载 1.修改配置文件 执行命令 :vi /etc/fstab 添加/dev/cdrom /mnt iso9660 ...
- 容器化的Apache服务并监控
1:基础容器编译部署apache 1.1:pull一个ubuntu 16.04的镜像 sudo docker pull ubuntu:16.04 1.2:运行容器 sudo docker run -d ...
- vue笔记 递归组件的使用
递归组件 什么是递归组件? 组件自身去调用组件自身. 代码示例 DetailList.vue(子组件-递归组件) <template> <div> <div class= ...