gpt4 book ai didi

c++ - 如何构建与操作系统隔离的 node-addon-api?

转载 作者:行者123 更新时间:2023-11-30 04:42:30 25 4
gpt4 key购买 nike

我有带有 nodejs 的后端服务器并部署在 heroku 上。

问题是我需要使用一些基于 fortran 的程序和 dll 文件(64 位和 32 位都存在,并使用 64 位)。

为了处理所有这些问题,我编写了一些 C++ 代码 (node-addon-api)。

带有 node-gyp 的 C++ 构建到 .node 文件,并且它在我的本地环境中工作。

但是,它在 heroku 上失败并出现以下错误。

Error: /app/refprop/refpropNapi.node: invalid ELF header

而且我找到了一些关于 ELF header 错误的解释。经过一番搜索后,我在下面找到了答案,它似乎非常适合我的情况。

This happens when you build on one architecture and then attempt to use the same built addon on a different architecture (or platform in some cases.

我的 node-addon-api 开发环境是 Windows 10,64 位。我的假设是 heroku 使用 Linux 操作系统,这就是编译的节点文件不起作用的原因。

最后,我有 3 个问题。

  1. 在Docker上编译C++和dll(我不知道如何在Docker中详细构建)是否会创建可以在heroku上执行的节点?

  2. 是否有必要对后端和 node-addon-api 进行 docker 化以确保它们在同一操作系统中运行?

  3. 看来使用docker部署让IaaS更容易,这让我逃避PaaS(heroku),但我没有任何经验。这会是逃离 heroku 的一种方式吗? (我不想再和服务器打交道了,因为我现在工作已经够多了……)

最佳答案

简答:
编译后的 .node 必须与其部署到的特定架构相匹配。在 docker 镜像中编译然后部署该 docker 镜像是一种方法,但它增加了复杂性,特别是围绕设置、使用和管理 docker 的额外步骤。使用 heroku 的 build 脚本为节点执行此操作的方法不那么复杂。 https://devcenter.heroku.com/changelog-items/1557https://devcenter.heroku.com/articles/nodejs-support#customizing-the-build-process

长答案:
这里有更多关于 Heroku 'dyno' 是什么的详细信息,它运行一个 Heroku 应用程序,作为背景。 https://stackoverflow.com/a/21463495https://devcenter.heroku.com/articles/how-heroku-works#building-applications

他们用于新应用程序的堆栈列在 https://devcenter.heroku.com/categories/stacks

目前是:

_________________________________________________________________
|Stack Version | Base Technology | Supported through |
|Heroku-18 (default) | Ubuntu 18.04 | April 2023 |
|Heroku-16 | Ubuntu 16.04 | April 2021 Learn more |
|Container | Docker | Developer-maintained* |
_________________________________________________________________

堆栈具有在 https://github.com/heroku/heroku-buildpack-nodejs 中描述的节点构建包构建您添加到 heroku 的节点应用程序。

您可以按照 https://devcenter.heroku.com/articles/nodejs-support#customizing-the-build-process 中的说明自定义构建过程和 https://devcenter.heroku.com/changelog-items/1557这意味着您可以添加一个构建脚本,每次将您的应用程序添加到 heroku 时该脚本都会运行。

"scripts": {
"start": "node index.js",
"build": "build steps here to compile your dll on heroku using node-gyp or npm build or npm install"
}

或者您可以使用 heroku-prebuild 或 heroku-postbuild 脚本。

"scripts": {
"heroku-prebuild": "echo This runs before Heroku installs your dependencies.",
"heroku-postbuild": "echo This runs afterwards."
}

https://nodejs.org/api/addons.html#addons_building更详细地描述了如何构建 C++ 插件。

免责声明:我不是 Heroku 员工,我最近也没有使用过 Heroku,所以我只是按照公开文档中的描述进行操作。


考虑到关于您可能想使用什么而不是 docker 的解释,以下是您列出的问题的答案:

  1. Would compiling C++ and dll on Docker (I don't know how to build in Docker in detail) would create node that can be executed on heroku?

是的。 Heroku 直接支持 docker 容器,或者您可以在与 Heroku 使用的内容相匹配的 docker 镜像(例如 Ubuntu 18.04)上编译并使用编译后的代码。

  1. Would it be NECESSARY to dockerize both backend and node-addon-api to ensure they run in same OS?

如果您使用的是 docker 容器,后端和 node-addon-api 都需要在同一个 docker 镜像中运行。如果你只在 docker 上编译,编译后的 c++ 需要匹配最终运行的操作系统节点。

  1. It seems that deploying using docker makes IaaS much easier, which makes me to escape PaaS (heroku), but I don't have any experience. Will it be a way to escape from heroku? (I don't want to work with server more, because I have enough work by now...)

是的。还有其他提供商提供可以运行节点服务器的 docker 容器服务。还有其他具有虚拟镜像的云提供商可以运行节点服务器而无需 dockerize。

关于c++ - 如何构建与操作系统隔离的 node-addon-api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58707269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com