简体   繁体   English

将Node本机模块部署到Azure WebSites

[英]Deployment of Node native modules to Azure WebSites

I'm trying to deploy the following sample node application to an Azure website: 我正在尝试将以下示例节点应用程序部署到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);

The tricky part is that I'm using a node module called " canvas " that needs to be compiled at install time using node-gyp. 棘手的部分是我正在使用一个名为“ canvas ”的节点模块,需要在安装时使用node-gyp进行编译。

According to this Microsoft page native modules are not supported on Azure Web-sites and the compiled version of the module should be copied to Azure for the app to work properly: 根据 Microsoft页面,Azure网站不支持本机模块,并且应将模块的已编译版本复制到Azure以使应用程序正常工作:

Native Modules 本机模块

While most modules are simply plain-text JavaScript files, some modules are platform-specific binary images. 虽然大多数模块只是纯文本JavaScript文件,但某些模块是特定于平台的二进制映像。 These modules are compiled at install time, usually by using Python and node-gyp. 这些模块在安装时编译,通常使用Python和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. Azure网站的一个特定限制是,虽然它本身了解如何安装package.json或npm-shrinkwrap.json文件中指定的模块,但它不提供Python或node-gyp,并且无法构建本机模块。

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. 由于Azure云服务依赖于作为应用程序一部分部署的node_modules文件夹,因此作为已安装模块的一部分包含的任何本机模块应在云服务中工作,只要它在Windows开发系统上安装和编译即可。

Native modules are not supported with Azure Web Sites. Azure网站不支持本机模块。 Some modules such as JSDOM and MongoDB have optional native dependencies, and will work with applications hosted in Azure Web Sites. 某些模块(如JSDOM和MongoDB)具有可选的本机依赖项,​​并且可以与Azure网站中托管的应用程序一起使用。

I deployed everything to Azure (using the publishing Wizard) and all the files were sent to Azure (apparently at least), including the compiled modules. 我将所有内容部署到Azure(使用发布向导),所有文件都发送到Azure(显然至少),包括已编译的模块。 When running the site on Azure I obtain the following exception: 在Azure上运行站点时,我获得以下异常:

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

But that file is in-fact deployed on the server: 但该文件实际上部署在服务器上:

文件夹上的文件

Creating a VM would obviously be an option but I would prefer to use web-sites. 创建VM显然是一种选择,但我更喜欢使用网站。 Anyone has any idea that I can try out? 任何人都知道我可以试试吗?

The error message ""The specified module could not be found" is a bit misleading. It can mean one or more dependent dlls are not found. 错误消息“”无法找到指定的模块“有点误导。它可能意味着找不到一个或多个相关的dll。

I assumed you used the build instruction in Windows. 我假设您在Windows中使用了构建指令 If you built canvas against the GTK2 zip file in the instruction, you can copy %GTKDIR%\\bin\\freetype6.dll to %YourApp%\\node_modules\\canvas\\build\\Release. 如果您在指令中针对GTK2 zip文件构建了canvas,则可以将%GTKDIR%\\ bin \\ freetype6.dll复制到%YourApp%\\ node_modules \\ canvas \\ build \\ Release。 This dll is not copied during the build process. 在构建过程中不会复制此dll。 You would need to do this deploying to a VM as well. 您还需要将此部署到VM。 Or you can build canvas against the Windows GTK3 bundle . 或者您可以针对Windows GTK3软件包构建画布。

And unfortunately even after you have all the dll, the sandbox of Azure Websites seems to prevent canvas from running. 不幸的是,即使你拥有了所有的dll,Azure网站的沙箱似乎也阻止了画布的运行。 Hopefully it would be enabled in the near future. 希望它能在不久的将来启用。

Alright got this working today! 好吧今天就搞定了!

You need to deploy your node_modules directory, in addition to that you need to copy all the contents gtk/bin to the root of your application, which makes it a bit messy, but it works. 你需要部署你的node_modules目录,除了你需要将所有内容gtk/bin复制到你的应用程序的根目录,这使它有点乱,但它的工作原理。 I imagine you could change the path to look elsewhere but I needed to get the app running so I didn't investigate any further. 我想你可以改变寻找其他地方的路径,但我需要让应用程序运行,所以我没有进一步调查。

That last line in @mtian's answser is key. @ mtian的回答中的最后一行是关键。 You can spend time getting it to deploy but ultimately the Azure Website VMs lack the GDI+ APIs that node-canvas relies on to execute (presumably blocked for security considerations). 您可以花时间部署它,但最终Azure网站虚拟机缺少节点 - 画布依赖执行的GDI + API(可能出于安全考虑而被阻止)。 If you want node-canvas on Azure, you can't use Azure Websites and will instead need to setup and manage your own VMs. 如果您想在Azure上使用node-canvas,则无法使用Azure网站,而是需要设置和管理自己的VM。

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

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