最近打算玩一下service worker,可是service worker只能在https下跑,因此查资料本身用纯express搭建了一个微服务器,把过程记录下来,供之后开发时参考,相信对其余人也有用。html
参考资料:express官方文档node
首先咱们用express搭建一个http服务器,很简单,看看官方文档就能够搭建出来了。代码以下:git
// 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文件夹。github
因此咱们加入代码解析静态资源:express
// 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); });
咱们想把http变成https,首先咱们要生成本地证书:api
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同理)app
添加完以后咱们把秘钥和公钥放在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); });
在项目中,咱们常常遇到跨域问题,在开发时咱们是经过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