我正在使用react-router,因此希望在AWS Ec2上进行托管。如何部署应用程序并使其永久在后台运行,或者如果有其他方式,请告诉我。
我正在使用react-router,因此希望在AWS Ec2上进行托管。如何部署应用程序并使其永久在后台运行,或者如果有其他方式,请告诉我。
您可以使用Amazon S3。
在本地实例中运行npm run build。
将文件上传到S3存储桶实例。
可以选择静态网站托管。
npm install -g pm2
安装pm2
步骤2:关闭正在运行的react服务器,使用sudo pm2 start server.js
运行应用程序。 - Prav使用它,您可以在同一台机器上部署多个React应用程序。
要部署应用程序,你需要什么?
1. Nodejs
2. PM2js
3. Koa.js
4. Koa-static.js
您只需按照以下步骤进行操作:
npm run build
。文件名:buildStart.js
const httpPort = 80;
const httpsPort = 443;
const koa = require( 'koa' );
const serve = require( 'koa-static' );
const http = require( 'http' );
const https = require( 'https' );
const fs = require( 'fs' );
const app = new koa();
const cert = fs.readFileSync( '/ssl/cert.crt' );
const key = fs.readFileSync( '/ssl/private.key' );
app.use( serve( __dirname + '/build', {
maxage: 365 * 24 * 60 * 60
} ) );
http.createServer( app.callback() ).listen( httpPort, () => console.log( `sever is listening on ${httpPort}` ) );
https.createServer( { cert, key }, app.callback() ).listen( httpsPort, () => console.log( `sever is listening on ${httpsPort}` ) );
以上代码将在HTTP和HTTPS两种协议下启动你的React应用程序。如果你没有HTTPS证书,那么只能在HTTP协议下启动。
const httpPort = 80;
const httpsPort = 443;
const koa = require( 'koa' );
const serve = require( 'koa-static' );
const http = require( 'http' );
const https = require( 'https' );
const fs = require( 'fs' );
const app = new koa();
app.use( serve( __dirname + '/build', {
maxage: 365 * 24 * 60 * 60
} ) );
http.createServer( app.callback() ).listen( httpPort, () => console.log( `sever is listening on ${httpPort}` ) );