Using custom HTTPS certificate in Nuxt (but only in development mode)?
P粉478445671
P粉478445671 2024-03-21 19:02:42
0
1
408

The dependencies I'm using require me to have HTTPS on localhost. I used the following code in nuxt.config.js to accomplish this:

server: {        
        https: {
            key: fs.readFileSync(path.resolve(__dirname, 'localhost-key.pem')),
            cert: fs.readFileSync(path.resolve(__dirname, 'localhost.pem'))
        }
    },

These are the keys I created myself using mkcert. However, I will use the actual certificate on the live page. Is there a way to restrict the server block in nuxt.config.js to development mode only?

P粉478445671
P粉478445671

reply all(1)
P粉521697419

I have used it before

server: {
    https: process.env.NODE_ENV === 'development' && process.env.USE_LOCAL_HTTPS === 'true'
      ? {
        key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
        cert: fs.readFileSync(path.resolve(__dirname, 'server.crt')),
      }
      : false,
},

NODE_ENV is used to double check that the environment is development and USE_LOCAL_HTTPS is another variable to make sure it is not staging development environment. Of course, you might not even need it if your NODE_ENV has something like staging or test.

Otherwise, I've never double checked, but this server key configuration might even only work for local development. Give it a try, otherwise try my configuration.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!