我尝试了这个并且成功了。
注意:我在 chrome 中使用 React、Express 和 Selenium
第 1 步:创建一个新的 Heroku 应用程序。
第 2 步:从您的终端,使用以下命令登录 Herokuheroku login
第 3 步:登录后,cd
到您的项目目录并将其远程设置为您的heroku应用程序。heroku git:remote -a YOUR-HEROKU-APP-NAME
第 4 步:在终端中运行以下所有命令
heroku buildpacks:add https://github.com/heroku/heroku-buildpack-chromedriver
heroku buildpacks:add https://github.com/heroku/heroku-buildpack-google-chrome
heroku config:set CHROME_DRIVER_PATH=/app/.chromedriver/bin/chromedriver
heroku config:set CHROME_BINARY_PATH=/app/.apt/opt/google/chrome/chrome
第 5 步:从浏览器登录 heroku 并导航到您的应用程序。转到设置并在下面buildpacks
, add heroku/nodejs
第 6 步:这就是我的 index.js 的样子。注意:我的快速入口点在里面root-dir/server/index.js
我的反应文件在里面root-dir/client/
const express = require('express');
const app = express();
const path = require('path');
// Serve static files from the React app.
app.use(express.static(path.join(__dirname, '..', 'client/build')));
app.get('/api', async (req, res) => {
const webdriver = require('selenium-webdriver');
require('chromedriver');
const chrome = require('selenium-webdriver/chrome');
let options = new chrome.Options();
options.setChromeBinaryPath(process.env.CHROME_BINARY_PATH);
let serviceBuilder = new chrome.ServiceBuilder(process.env.CHROME_DRIVER_PATH);
//Don't forget to add these for heroku
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
let driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.setChromeService(serviceBuilder)
.build();
await driver.get('http://www.google.com');
res.send(await driver.getTitle());
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '..', 'client/build/index.html'));
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`listening to port ${port} now...`);
});
第 7 步(如果您使用的是 React):现在在您的package.json
in root-dir/
,添加这个
"scripts": {
...
"heroku-postbuild": "cd client && npm install && npm run build"
}
第 8 步(如果您使用的是 React):在您的package.json
in root-dir/client/
(i.e: package.json
对于反应应用程序),添加以下行:
"proxy": "http://localhost:5000/",
第8步:(如果你使用的是react):里面root-dir/client/src/
,创建一个新文件,名为setupProxy.js
并粘贴以下代码:
const proxy = require("http-proxy-middleware");
module.exports = function(app) {
app.use(proxy('/api', { target: `http://localhost:${process.env.PORT || 5000}/`}));
};
步骤 9: 现在,您已准备好进行部署。确保您已安装以下软件包:express
, selenium-webdriver
, and chromedriver
第10步:现在将其推送到heroku
git add .
git commit -m "my app"
git push heroku master