Webpack Dev Server running on HTTPS/Web Sockets Secure

前端 未结 6 885
無奈伤痛
無奈伤痛 2020-12-02 18:19

Normally in developer mode Webpack runs using HTTP. There is usually a web server serving content through HTTP and webpack using http/websockets on a separate port.

6条回答
  •  悲&欢浪女
    2020-12-02 18:26

    I'm working on react project, Now wanted to add SSL certificate on this project and run my website with https so have followed below step:

    1. In add https in webpack.config.js

       devServer:{
      
                https: true,
                host: '0.0.0.0', // you can change this ip with your ip
                port: 443,       // ssl defult port number
                inline: true,
      
                historyApiFallback: true,
                publicPath: '/',
                contentBase: './dist',
                disableHostCheck: true
         }
      
    2. Add SSL public certificate on package.json file If you didn't want to add a certificate on your package.json file then you have to add it on your webpack.config.js it is mandatory to add your certificate in your project either you can it on package.json file or webpack.config.js

    For Package.json

    scripts: {
                        "test": "echo \"Error: no test specified\" && exit 1",
                        "build": "webpack --mode production",
                        "start": "webpack-dev-server  --open --https --cert /path/to/private.crt --key /path/to/private.key"
    
                }
    

    OR webpack.config.js

     devServer:{
    
                  https: true,
                  host: '0.0.0.0', // you can change this ip with your ip
                  port: 443,       // ssl defult port number
                  inline: true,
                  https: {
                        key: fs.readFileSync('/path/to/private.pem'),
                        cert: fs.readFileSync('/path/to/private.pem'),
                        ca: fs.readFileSync('/path/to/private.pem')
                        }
                  historyApiFallback: true,
                  publicPath: '/',
                  contentBase: './dist',
                  disableHostCheck: true
           }
    
    1. Run npm start command on a terminal or you can also use pm2 start npm -- start

提交回复
热议问题