使用NextJS + Express在localhost上进行HTTPS

reactjs React.js

西门

2020-03-24

系统信息

目标

在本地主机上通过HTTPS使用SSL服务Web应用程序

已经做了什么

  1. 使用Create Next App创建基本的NextJS应用程序
  2. 使用OpenSSL生成证书和密钥,并将其移至项目目录
  3. 添加了Express依赖
  4. 将应用程序配置为在内部使用Express server.js
  5. 更改了脚本以使用server.js内部package.json脚本。

server.js

const express = require('express');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const port = 3000;

const https = require('https');
const fs = require('fs');
const httpsOptions = {
  key: fs.readFileSync('./certificates/key.pem'),
  cert: fs.readFileSync('./certificates/cert.pem')
};

app
  .prepare()
  .then(() => {
    const server = express();

    server.get('*', (req, res) => {
      return handle(req, res);
    });

    server.listen(port, err => {
      if (err) throw err;
      console.log('> Ready on http://localhost: ' + port);
    });
  })
  .catch(ex => {
    console.error(ex.stack);
    process.exit(1);
  });

额外的信息

使用初始化时,该应用当前可以运行yarn dev我尝试使用此答案通过https服务该应用程序,但无法弄清楚如何使用NextJS将其应用于当前设置。

我花了很多时间研究网络上如何应用此解决方案,但还没有找到实现此工作的方法。

任何帮助是极大的赞赏。

第3378篇《使用NextJS + Express在localhost上进行HTTPS》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

0个回答

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android