如何让Create-React-App的应用自动支持HTTPS?
来源:互联网 发布:js添加图片二维数组 编辑:程序博客网 时间:2024/06/05 03:43
我们知道,我们在使用React框架的时候,可以通过Create-React-App命令生成一个React的应用。但是有的时候,我们想让其支持HTTPS,应该如何做呢?
其实不难,只要我们设置在系统的环境变量里面设置了,HTTPS=TRUE
PORT=443就行了。
那么为什么呢? 因为其实我们在控制台运行npm start的时候,其会调用create-react-app命令给我们封装好了脚本。其就在react-scripts 模块的 子文件夹 /scripts 下面。找到start.js的脚本,我们会看到下面的信息:
// @remove-on-eject-begin/** * Copyright (c) 2015-present, Facebook, Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */// @remove-on-eject-end'use strict';// Do this as the first thing so that any code reading it knows the right env.process.env.BABEL_ENV = 'development';process.env.NODE_ENV = 'development';// Makes the script crash on unhandled rejections instead of silently// ignoring them. In the future, promise rejections that are not handled will// terminate the Node.js process with a non-zero exit code.process.on('unhandledRejection', err => { throw err;});// Ensure environment variables are read.require('../config/env');const fs = require('fs');const chalk = require('chalk');const webpack = require('webpack');const WebpackDevServer = require('webpack-dev-server');const clearConsole = require('react-dev-utils/clearConsole');const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');const { choosePort, createCompiler, prepareProxy, prepareUrls,} = require('react-dev-utils/WebpackDevServerUtils');const openBrowser = require('react-dev-utils/openBrowser');const paths = require('../config/paths');const config = require('../config/webpack.config.dev');const createDevServerConfig = require('../config/webpackDevServer.config');const useYarn = fs.existsSync(paths.yarnLockFile);const isInteractive = process.stdout.isTTY;// Warn and crash if required files are missingif (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { process.exit(1);}// Tools like Cloud9 rely on this.const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;const HOST = process.env.HOST || '0.0.0.0';// We attempt to use the default port but if it is busy, we offer the user to// run on a different port. `detect()` Promise resolves to the next free port.choosePort(HOST, DEFAULT_PORT) .then(port => { if (port == null) { // We have not found a port. return; } const protocol = process.env.HTTPS === 'true' ? 'https' : 'http'; const appName = require(paths.appPackageJson).name; const urls = prepareUrls(protocol, HOST, port); // Create a webpack compiler that is configured with custom messages. const compiler = createCompiler(webpack, config, appName, urls, useYarn); // Load proxy config const proxySetting = require(paths.appPackageJson).proxy; const proxyConfig = prepareProxy(proxySetting, paths.appPublic); // Serve webpack assets generated by the compiler over a web sever. const serverConfig = createDevServerConfig( proxyConfig, urls.lanUrlForConfig ); const devServer = new WebpackDevServer(compiler, serverConfig); // Launch WebpackDevServer. devServer.listen(port, HOST, err => { if (err) { return console.log(err); } if (isInteractive) { clearConsole(); } console.log(chalk.cyan('Starting the development server...\n')); openBrowser(urls.localUrlForBrowser); }); ['SIGINT', 'SIGTERM'].forEach(function(sig) { process.on(sig, function() { devServer.close(); process.exit(); }); }); }) .catch(err => { if (err && err.message) { console.log(err.message); } process.exit(1); });
其中process.env.PORT和process.env.HTTPS分别用来设置启动后的端口以及是否启用HTTPS。如果在环境变量一旦设置,其在第一次启动的时候就会生成一个服务器端的自签名的SSL的证书,从而应用HTTPS。
阅读全文
0 0
- 如何让Create-React-App的应用自动支持HTTPS?
- 开发 react 应用最好用的脚手架 create-react-app
- 如何让你的Apache支持https
- create-react-app 支持多入口
- heroku 自动部署create-react-app项目
- 使用create-react-app自动构建react项目
- 利用 Create React Native App 快速创建 React Native 应用
- 解决:使用create-react-app构建react应用很慢
- 如何让应用支持 Android 8.0 自动填充?
- create-react-app my-appcreate-react-app慢的解决方法
- create-react-app慢的解决方法
- 升级Create React App的项目
- create-react-app路由的实现原理
- react脚手架create-react-app
- 如何让boa网页服务器支持HTTPS
- 如何让你的Genymotion支持ARM架构APP
- 让 svn 支持 https 协议的 checkout
- 让svn支持https协议的checkout
- Java中Unsafe类详解
- 第四章 排序:智能收件箱
- 红茶一杯话Binder 一(初始篇)
- 将大图片完整的放到小div里
- 2018年最新Spring Boot视频教程附代码笔记资料(50G)
- 如何让Create-React-App的应用自动支持HTTPS?
- 开源镜像网站汇总
- ajax之后on绑定事件,第一次ajax可以。第二次点击不行。on执行两次
- 【JavaScript】&&和||的另类用法
- 51Nod 1344 走格子 模拟
- 条件结构之switch语句
- Cortex-M3异常中断及向量表定义
- Linux 安装常见问题
- javascript高级程序设计 读书笔记(一)