node.js - 将 Node native 模块部署到 Azure 网站

标签 node.js azure azure-web-app-service node-modules azure-deployment

我正在尝试将以下示例 Node 应用程序部署到 Azure 网站:

var http = require('http');
var port = process.env.port || 1337;

http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/html' });

    try {

        var canvasModule = require('canvas'), 
            canvas = new canvasModule(200, 200), 
            ctx = canvas.getContext('2d');

        ctx.rotate(0.5);

        ctx.font = '30px Impact';
        var message = "Hello World";
        ctx.fillText(message, 40, 40);

        ctx.strokeRect(30, 5, 160, 50);

        res.end('<html><img src="' + canvas.toDataURL() + '" /></html>');

    } catch (e) {
        res.end('Error: ' + e);
    }

}).listen(port);

棘手的部分是我正在使用一个名为“canvas ”的 Node 模块,需要在安装时使用 node-gyp 进行编译。

根据this Azure 网站不支持 Microsoft 页面 native 模块,应将模块的编译版本复制到 Azure 以使应用程序正常工作:

Native Modules

While most modules are simply plain-text JavaScript files, some modules are platform-specific binary images. These modules are compiled at install time, usually by using Python and node-gyp. One specific limitation of Azure Web Sites is that while it natively understands how to install modules specified in a package.json or npm-shrinkwrap.json file, it does not provide Python or node-gyp and cannot build native modules.

Since Azure Cloud Services rely on the node_modules folder being deployed as part of the application, any native module included as part of the installed modules should work in a cloud service as long as it was installed and compiled on a Windows development system.

Native modules are not supported with Azure Web Sites. Some modules such as JSDOM and MongoDB have optional native dependencies, and will work with applications hosted in Azure Web Sites.

我将所有内容部署到 Azure(使用发布向导),并且所有文件都发送到 Azure(显然至少如此),包括编译的模块。在 Azure 上运行该站点时,出现以下异常:

Error: The specified module could not be found.
D:\home\site\wwwroot\node_modules\canvas\build\Release\canvas.node

但该文件实际上已部署在服务器上:

files on folder

创建虚拟机显然是一种选择,但我更喜欢使用网站。有人知道我可以尝试一下吗?

最佳答案

错误消息“无法找到指定的模块”有点误导。它可能意味着未找到一个或多个依赖的 dll。

我假设您使用了 build instruction在Windows中。如果您根据说明中的 GTK2 zip 文件构建 Canvas ,则可以将 %GTKDIR%\bin\freetype6.dll 复制到 %YourApp%\node_modules\canvas\build\Release。在构建过程中不会复制此 dll。您还需要执行此部署到虚拟机的操作。或者您可以根据Windows GTK3 bundle构建 Canvas 。 .

不幸的是,即使您拥有所有 dll,Azure 网站的沙箱似乎仍会阻止 Canvas 运行。希望它能在不久的将来启用。

关于node.js - 将 Node native 模块部署到 Azure 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25142980/

相关文章:

azure - Terraform - 用于资源创建的嵌套循环

azure - 在 Azure Web App for Containers 上使用 ASP.Net Core 配置 AppSettings : Whither Colons?

asp.net-mvc - Azure 每 5 分钟点击一次 Web 应用程序主页

node.js - 如何加快 Mongoose 中的 MongoDB 查询速度?

node.js - Docker错误: no such file or directory,打开 '/package.json'

javascript - azure-graph 抛出 "Access Token missing or malformed"

c# - 使用客户端证书时,大型 POST 请求会导致 Asp.Net Web API 超时

git - 在 Node.js 中无需控制台和调试语句即可推送到生产环境的正确方法?

azure - 从 Terraform 在 Azure 存储帐户中设置 CORS

azure - 需要将 Azure 应用程序 Web 服务连接到本地 SQL 数据库