当前位置:网站首页>pm2 static file service

pm2 static file service

2022-08-10 14:54:00 choukin

Respect individual survival experience,Connect independent thinking ability,Bridging the boundaries between knowledge and ability ———— 《我的二本学生》

通过 http The protocol serves static files

PM2 可以通过命令 pm2 serve Enable the static file serving function.It supports access to multiple raw files provided in a specified folder,Or as a single page application.

CLI

使用简单的命令,通过httpto serve static files(like a front-end app)

pm2 serve <path> <port>

默认端口是8080 如果没有指定<path> The current folder will be used.It can also be used like any other app--name 或者 --watch

单页面应用:All requests are redirected to index.html

使用--spaoption to redirect all requests to index.html

pm2 serve --spa

使用配置文件

// ecosystem.config.js 
module.exports = {
    apps : [{
      script:"serve",
      env:{
          PM2_SERVE_PATH:'.',
          PM2_SERVE_PORT:8080,
          PM2_SERVE_SPAtrue,
          PM2_SERVE_HOMEPAGE:'./index.html',
      }
    }]
  }

Execute the following command and access http://localhost:8080

pm2 start ecosystem.config.js

Access rights are set by using a password

To protect access to public files you can use the following command

pm2 serve --basic-auth-username <username> --basic-auth-password <password>

配置文件方式

// ecosystem.config.js
module.exports = {
    script:"serve",
    env:{
        PM2_SERVE_PATH:'.',
        PM2_SERVE_PORT:8080,
        PM2_SERVE_SPA:'./index.html',
        PM2_SERVE_BASIC_AUTH'true',
        PM2_SERVE_BASIC_AUTH_USERNAME'example-login',
        PM2_SERVE_BASIC_AUTH_PASSWORD'example-password'        
    }
}
原网站

版权声明
本文为[choukin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101431162057.html