如何将部署Selenium NodeJS Web应用程序部署到Heroku?

时间:2017-10-14 19:50:01

标签: node.js selenium heroku selenium-chromedriver

我在Local创建了一个Selenium NodeJS网络应用。它使用chromedriver,我的驱动程序必须使用一些Chrome扩展程序。当地的一切都很好。 我想在Heroku中使用它,但我不能这样做。我尝试使用构建包,但我不能再做了。

如何将其部署到Heroku?

package.json(dependencies):

..
  "dependencies": {
    "body-parser": "*",
    "express": "*",
    "firebase": "^4.1.5",
    "firebase-admin": "^4.2.1",
    "selenium-webdriver": "*",
    "chromedriver":"*",
    "telebot":"*"
  },
..

2 个答案:

答案 0 :(得分:2)

粘贴我的最后一个答案:

注意:我正在使用带有铬的React,Express和Selenium

第1步:创建一个新的Heroku应用。

步骤2:在您的终端上,使用heroku login

登录到heroku

第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下,添加heroku/nodejs

第6步:这就是我的index.js的样子。注意:我的快速入口点位于root-dir/server/index.js内部,而我的react文件位于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中的root-dir/内,添加此

"scripts": {
...
"heroku-postbuild": "cd client && npm install && npm run build"
}

第8步(如果您使用的是react):在package.json中的root-dir/client/内(即:React应用为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步:现在,您可以进行部署了。确保已安装以下软件包:expressselenium-webdriverchromedriver

步骤10:现在将其推送到heroku

git add .
git commit -m "my app"
git push heroku master

您完成了!

答案 1 :(得分:-1)

您可以执行以下步骤在heroku上部署您的应用程序

1)在git上创建一个新的存储库      在github上创建一个帐户并创建一个新的存储库      然后在命令行上使用

检查项目文件的状态
git status 

如果是红色标记,则使用

添加所有文件
git add .

然后提交

git commit -m "first commit"

2)推送该存储库中的所有数据

git remote add origin https://github.com/git_account/repository_name.git
   git push -u origin master

3)然后从git推送到heroku

git push heroku