反应快速 pm2 部署
react express pm2 deployment
场景
我已经为 API 制作了 React 应用程序和一个快速服务器,两者是分开的。我没有在 Express 应用程序中包含 React 文件夹。我期待使用 PM2 使用 pre/post 脚本部署它,但我很难完全实现我的想法。
目标
- 我想 运行 为客户端和服务器安装 npm,如果以后需要的话,我可能 remove/add 打包。
- 我想在 npm install 之后,我想构建 React 应用程序,然后移动该文件夹以用于表达(我不知道是否可以为表达静态内容提供父目录之外的目录路径).
- 然后我想启动最终将提供反应构建文件的快速服务器。
现在我的目录结构是
.
├── client
├── ecosystem.config.js
└── server
我很困惑,因为我没有遇到实现此目的的任何资源。另外我不确定这是否可以使用 pm2 部署脚本,或者我需要编写自己的 bash 脚本来做一些事情,然后 pm2 将只启动服务器。
这只是我所做的,似乎完全错误
ecosystem.config.js
module.exports = {
apps : [{
name: 'API',
cwd: 'server',
script: 'server.js',
args: 'one two',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env_production: {
NODE_ENV: 'production'
}
}],
deploy : {
production : {
user : 'node',
// host : '212.83.163.1',
// ref : 'origin/master',
// repo : 'git@github.com:repo.git',
// path : '/var/www/production',
'post-deploy' : 'cd client && npm run build'
}
}
};
我对你的问题的看法:
客户端和服务器端可以package.json组合。
我对你的问题有些困惑。
- 您可以配置为:
module.exports = {
apps : [{
name: 'API',
cwd: 'server',
script: 'npm run react && npm run express', //you must config npm run react && npm run express before use them
args: 'one two',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env_production: {
NODE_ENV: 'production'
}
}],
deploy : {
production : {
user : 'node',
// host : '212.83.163.1',
// ref : 'origin/master',
// repo : 'git@github.com:repo.git',
// path : '/var/www/production',
'post-deploy' : 'cd client && npm run build'
}
}
}
如果它不能解决您的问题,请告诉我。
因为你同时拥有服务器和客户端,我假设它是在 monorepo 中开发的。
出于这些目的,我建议使用 yarn workspaces,因为它可以满足您的第一个要求(npm install 用于客户端和服务器)。
要在 1.0 之前的 yarn 版本中启用工作区,请执行启用它。
yarn config set workspaces-experimental true
然后在服务器和客户端文件夹外的文件夹(工作区根目录)中添加一个 package.json
以及您需要的任何其他 package.json
键。还可以直接在此处安装公共依赖项,而不是在服务器和客户端的 package.json 文件中单独安装它们。 (不要忘记删除其中的 node_modules
文件夹,因为将在工作区根目录中创建一个新文件夹,并在 yarn 安装期间一起安装所有依赖项)。
{
"private": true,
"workspaces": ["server", "client"]
}
将以下内容添加到您的服务器 index.js 文件。
app.use(express.static(path.join(__dirname, 'public')));
app.get('*', (req,res) =>{
res.sendFile(path.join(__dirname+'/public/index.html'));
});
并将 npm 脚本添加到工作区根目录中的 package.json
。
{
...
"scripts": {
"start": "yarn --cwd client react-scripts build && mv ./client/build ./server/public && node ./server/index.js"
}
...
}
场景
我已经为 API 制作了 React 应用程序和一个快速服务器,两者是分开的。我没有在 Express 应用程序中包含 React 文件夹。我期待使用 PM2 使用 pre/post 脚本部署它,但我很难完全实现我的想法。
目标
- 我想 运行 为客户端和服务器安装 npm,如果以后需要的话,我可能 remove/add 打包。
- 我想在 npm install 之后,我想构建 React 应用程序,然后移动该文件夹以用于表达(我不知道是否可以为表达静态内容提供父目录之外的目录路径).
- 然后我想启动最终将提供反应构建文件的快速服务器。
现在我的目录结构是
.
├── client
├── ecosystem.config.js
└── server
我很困惑,因为我没有遇到实现此目的的任何资源。另外我不确定这是否可以使用 pm2 部署脚本,或者我需要编写自己的 bash 脚本来做一些事情,然后 pm2 将只启动服务器。
这只是我所做的,似乎完全错误
ecosystem.config.js
module.exports = {
apps : [{
name: 'API',
cwd: 'server',
script: 'server.js',
args: 'one two',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env_production: {
NODE_ENV: 'production'
}
}],
deploy : {
production : {
user : 'node',
// host : '212.83.163.1',
// ref : 'origin/master',
// repo : 'git@github.com:repo.git',
// path : '/var/www/production',
'post-deploy' : 'cd client && npm run build'
}
}
};
我对你的问题的看法:
客户端和服务器端可以package.json组合。
我对你的问题有些困惑。
- 您可以配置为:
module.exports = {
apps : [{
name: 'API',
cwd: 'server',
script: 'npm run react && npm run express', //you must config npm run react && npm run express before use them
args: 'one two',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env_production: {
NODE_ENV: 'production'
}
}],
deploy : {
production : {
user : 'node',
// host : '212.83.163.1',
// ref : 'origin/master',
// repo : 'git@github.com:repo.git',
// path : '/var/www/production',
'post-deploy' : 'cd client && npm run build'
}
}
}
如果它不能解决您的问题,请告诉我。
因为你同时拥有服务器和客户端,我假设它是在 monorepo 中开发的。
出于这些目的,我建议使用 yarn workspaces,因为它可以满足您的第一个要求(npm install 用于客户端和服务器)。
要在 1.0 之前的 yarn 版本中启用工作区,请执行启用它。
yarn config set workspaces-experimental true
然后在服务器和客户端文件夹外的文件夹(工作区根目录)中添加一个 package.json
以及您需要的任何其他 package.json
键。还可以直接在此处安装公共依赖项,而不是在服务器和客户端的 package.json 文件中单独安装它们。 (不要忘记删除其中的 node_modules
文件夹,因为将在工作区根目录中创建一个新文件夹,并在 yarn 安装期间一起安装所有依赖项)。
{
"private": true,
"workspaces": ["server", "client"]
}
将以下内容添加到您的服务器 index.js 文件。
app.use(express.static(path.join(__dirname, 'public')));
app.get('*', (req,res) =>{
res.sendFile(path.join(__dirname+'/public/index.html'));
});
并将 npm 脚本添加到工作区根目录中的 package.json
。
{
...
"scripts": {
"start": "yarn --cwd client react-scripts build && mv ./client/build ./server/public && node ./server/index.js"
}
...
}