Azure 应用服务上带有 Node-sass 的 Express 应用

Express app with Node-sass on Azure App Service

我有一个使用 express 的基本节点 Web 应用程序,它依赖于 node-sass 库。

这是在 Win64 服务器上构建的,因此在构建的 npm 安装部分,由于当前环境,它正在下载绑定二进制文件的 x64 版本。

当它部署到 Azure 应用服务时,由于与 node-sass 绑定二进制文件不兼容,它会抛出运行时错误,因为节点在 Azure 应用服务中运行 32 位...

Error: Missing binding D:\home\site\wwwroot\node_modules\node-sass\vendor\win32-ia32-48\binding.node Node Sass could not find a binding for your current environment: Windows 32-bit with Node.js 6.x

Found bindings for the following environments: - Windows 64-bit with Node.js 6.x

当我显式签入 the 32bit binding 并重新部署时,我有时会收到 502 网关错误...

502 - Web server received an invalid response while acting as a gateway or proxy server. There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

有时我只是得到 500,但它不再将错误写入日志。

该应用程序显式依赖于 node-sass-middleware 包版本 0.11,这依赖于 node-sass 4.3.0.

没有任何错误日志,我就走到了死胡同。您以前遇到过这个问题吗?如果遇到过,您是如何解决的?

我利用 Node-Sass Example App to have quick test, used local git 将它的示例项目部署到 Azure Web 应用程序,这重现了您的问题。

通过部署日志:

remote: Selected node.js version 7.4.0. Use package.json file to choose a different version. remote: Selected npm version 4.0.5

并根据类似的错误信息:

Found bindings for the following environments: - Windows 64-bit with Node.js 6.x

我将 package.json 中的 node.js 版本指定为:

"engines": {
  "node": "= 6.9.1",
  "npm": "> 3"
}

然后通过本地git将其重新部署到Azure,示例工作正常。

对于您进一步的 500 错误,您可以尝试利用 应用服务编辑器 检查您网站的输出。

从 Azure 门户进入 App Service Editor,通过单击 show output[= 切换到 output 部分35=]按钮,然后点击运行启动应用程序。

我们最终通过将 node-sass-middleware 替换为 gulp-sass 并为 node-sass 添加了一个 npm 重建步骤来解决了这个问题。这里的主要区别在于 css 现在在构建过程中通过 gulp 呈现。 运行 npm rebuild node-sass 首先会调用绑定下载到构建服务器(如果需要),然后一个单独的任务会调用 gulp 任务来呈现 css.

我们问题的其余部分是由于 web.config 指定 app.js 作为入口点,但是 express4 使用 bin/www 文件,并且简单地引用 app.js. bin/www 作为入口点的问题是 iisnode 现在使用 bin 作为工作目录,这导致了根相对引用的问题。

我们没有再浪费时间来弄清楚是否可以配置不同的工作目录,我们只是将 bin/www 移动到 ./server.js 并将 web.config 更改为指向至 server.js

Express 应用程序现在可以在 Azure 网站上按预期运行。