简体   繁体   English

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

[英]How to build node-addon-api insulated from OS?

I have backend server with nodejs and deployed on heroku.我有带有 nodejs 的后端服务器,并部署在 heroku 上。

Problem is that I need to use some fortran-based program with dll file (both 64-bit and 32-bit exists, and used 64-bit).问题是我需要使用一些基于 fortran 的程序和 dll 文件(64 位和 32 位都存在,并且使用 64 位)。

To handle them all, I wrote some C++ code(node-addon-api).为了处理所有这些,我编写了一些 C++ 代码(node-addon-api)。

C++ with node-gyp builds to.node file, and it worked in my local environment. C++ 与 node-gyp 构建 to.node 文件,它在我的本地环境中工作。

However, it failed on heroku with following errors.但是,它在 heroku 上失败并出现以下错误。

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

And I found some explanation about ELF header error.我找到了一些关于 ELF header 错误的解释。 After some searching, I found answer below and it seems precisely fits to my case.经过一番搜索,我在下面找到了答案,它似乎完全适合我的情况。

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. 当您在一个架构上构建,然后尝试在不同的架构(或某些情况下的平台)上使用相同的构建插件时,就会发生这种情况。

My development environment for node-addon-api was Windows 10, 64 bit.我的 node-addon-api 开发环境是 Windows 10、64 位。 My assumption is heroku uses Linux OS, and that is why compiled node file do not work.我的假设是 heroku 使用 Linux 操作系统,这就是编译的节点文件不起作用的原因。

In conclusion, I have 3 questions.总之,我有3个问题。

  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? 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?

  2. Would it be NECESSARY to dockerize both backend and node-addon-api to ensure they run in same OS?是否有必要对后端和 node-addon-api 进行 dockerize 以确保它们在相同的操作系统中运行?

  3. It seems that deploying using docker makes IaaS much easier, which makes me to escape PaaS (heroku), but I don't have any experience.似乎使用 docker 进行部署使 IaaS 更容易,这使我能够逃离 PaaS (heroku),但我没有任何经验。 Will it be a way to escape from heroku?这会是一种逃离 heroku 的方法吗? (I don't want to work with server more, because I have enough work by now...) (我不想更多地使用服务器,因为我现在有足够的工作......)

Short answer:简短的回答:
The compiled.node must match the specific architecture it's being deployed to. compiled.node 必须与它被部署到的特定架构相匹配。 Compiling in a docker image and then deploying that docker image is one way to do this, but it adds complexity, specifically around the extra steps setting up, using, and managing docker.在 docker 映像中编译,然后部署该 docker 映像是执行此操作的一种方法,但它会增加复杂性,特别是围绕设置、使用和管理 Z05B6053C41A2130AFDZ6FC3B158BDA4E 的额外步骤。 There are less complex ways to do this for node using heroku's build scripts.使用 heroku 的build脚本对节点执行此操作的方法不太复杂。 https://devcenter.heroku.com/changelog-items/1557 and https://devcenter.heroku.com/articles/nodejs-support#customizing-the-build-process https://devcenter.heroku.com/changelog-items/1557 and https://devcenter.heroku.com/articles/nodejs-support#customizing-the-build-process

Long Answer:长答案:
Here's more detail about what what a Heroku 'dyno' is, which runs a Heroku app, as background.这里有更多关于什么是 Heroku 'dyno' 的详细信息,它运行 Heroku 应用程序作为背景。 https://stackoverflow.com/a/21463495 and https://devcenter.heroku.com/articles/how-heroku-works#building-applications https://stackoverflow.com/a/21463495https://devcenter.heroku.com/articles/how-herokuapplications-works

The stacks they use for new apps are listed at https://devcenter.heroku.com/categories/stacks他们用于新应用程序的堆栈列在https://devcenter.heroku.com/categories/stacks

Currently this is:目前这是:

_________________________________________________________________
|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*  |
_________________________________________________________________

The stacks have node buildpacks described at https://github.com/heroku/heroku-buildpack-nodejs that build the node app you add to heroku.这些堆栈具有在https://github.com/heroku/heroku-buildpack-nodejs中描述的节点构建包,用于构建您添加到 heroku 的节点应用程序。

You can customize the build process as described at https://devcenter.heroku.com/articles/nodejs-support#customizing-the-build-process and https://devcenter.heroku.com/changelog-items/1557 That means you can add a build script that will run each time your application is added to heroku. You can customize the build process as described at https://devcenter.heroku.com/articles/nodejs-support#customizing-the-build-process and https://devcenter.heroku.com/changelog-items/1557 That means您可以添加一个构建脚本,该脚本将在每次将您的应用程序添加到 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"
}

or you can use heroku-prebuild or heroku-postbuild scripts.或者您可以使用 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 describes in more detail how to build C++ addons. https://nodejs.org/api/addons.html#addons_building更详细地描述了如何构建 C++ 插件。

Disclaimer: I am not a Heroku employee nor have I used Heroku recently, so I am only going by what is described in the publicly available documentation.免责声明:我不是 Heroku 的员工,我最近也没有使用过 Heroku,所以我只按照公开文档中的描述进行。


With that explanation in mind as to what you might want to use instead of docker, here are the answers to your listed questions:考虑到您可能想要使用什么而不是 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? 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?

Yes.是的。 Heroku supports docker containers directly or you can compile on an docker image that matches what Heroku uses (eg Ubuntu 18.04) and use that compiled code. Heroku supports docker containers directly or you can compile on an docker image that matches what Heroku uses (eg Ubuntu 18.04) and use that compiled code.

  1. Would it be NECESSARY to dockerize both backend and node-addon-api to ensure they run in same OS?是否有必要对后端和 node-addon-api 进行 dockerize 以确保它们在相同的操作系统中运行?

Both the backend and node-addon-api would need to run in the same docker image if you are using a docker container.如果您使用的是 docker 容器,则后端和 node-addon-api 都需要在同一个 docker 映像中运行。 If you only compile on docker, the compiled c++ would need to match what OS node is eventually run on.如果您只在 docker 上编译,则编译后的 c++ 需要匹配最终运行的 OS 节点。

  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.似乎使用 docker 进行部署使 IaaS 更容易,这使我能够逃离 PaaS (heroku),但我没有任何经验。 Will it be a way to escape from heroku?这会是一种逃离 heroku 的方法吗? (I don't want to work with server more, because I have enough work by now...) (我不想更多地使用服务器,因为我现在有足够的工作......)

Yes.是的。 There are other providers that provide docker container services that can run node servers.还有其他提供者提供可以运行节点服务器的 docker 容器服务。 There are also other cloud providers with virtual images that can run node servers without the need to dockerize as well.还有其他具有虚拟映像的云提供商可以运行节点服务器,而无需进行 dockerize。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM