简体   繁体   English

使用IISNode将loopback.io生成的主机API生成到Azure

[英]Hosting API generated using loopback.io to Azure using IISNode

So i am trying something undocumented. 所以我正在尝试一些无证件的东西。 I built a simple rest API using loopback.io in nodejs and now i want to host it to azure websites. 我在nodejs中使用loopback.io构建了一个简单的rest API,现在我想将它托管到azure网站。 I forked the https://github.com/strongloop/loopback-getting-started and followed steps as described on https://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac/ 我分叉了https://github.com/strongloop/loopback-getting-started并遵循https://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-中所述的步骤。 部署-MAC /

I was able to push the files and get a deployment but then i am not sure how IISNode would start the application, because the server.js is located under /server/server.js IISNode fails to find a server.js at the root and doesn't create a web.config. 我能够推送文件并获得部署,但后来我不确定IISNode将如何启动应用程序,因为server.js位于/server/server.js下IISNode无法在根目录下找到server.js不会创建web.config。

I came across this Point iisnode at server.js file nested in folder in an iis website but am unable to make it work locally as well. 我遇到了嵌入iis网站文件夹中的server.js文件中的Point iisnode,但是我无法让它在本地工作。 This is what i tried 这是我试过的

  • create a server.js on the root level with contents 使用内容在根级别创建server.js.

 require(__dirname + '\\\\server\\\\server.js')(); 

  • noticed that loopback starts the application with "node .", i inferred that it uses main module define in packages.json so i also changed the main module location from "/server/server.js" to "server.js" 注意到loopback用“node。”启动应用程序,我推断它使用packages.json中的主模块定义,所以我也将主模块位置从“/server/server.js”更改为“server.js”

 { "name": "Sample", "version": "1.0.0", "main": "server.js", "scripts": { "pretest": "jshint ." }, "dependencies": { "compression": "^1.0.3", "cors": "^2.5.2", "errorhandler": "^1.1.1", "loopback": "^2.14.0", "loopback-boot": "^2.6.5", "loopback-datasource-juggler": "^2.19.0", "serve-favicon": "^2.0.1" }, "optionalDependencies": { "loopback-explorer": "^1.1.0" }, "devDependencies": { "jshint": "^2.5.6" }, "repository": { "type": "", "url": "" }, "description": "Sample" } 

after these two changes when i run "node ." 当我运行“节点”时,在这两个更改之后。 i find the following error 我发现以下错误

C:\Users\anirudh\Documents\GitHub\Sample\node_modules\loopback\node_modules\express\lib\router\index.js:138
  debug('dispatching %s %s', req.method, req.url);
                                ^
TypeError: Cannot read property 'method' of undefined
    at Function.handle (C:\Users\anirudh\Documents\GitHub\Sample\node_modules\loopback\node_modules\express\lib\router\index.js:138:33)
    at EventEmitter.handle (C:\Users\anirudh\Documents\GitHub\Sample\node_modules\loopback\node_modules\express\lib\application.js:173:10)
    at app (C:\Users\anirudh\Documents\GitHub\Sample\node_modules\loopback\node_modules\express\lib\express.js:38:9)
    at Object.<anonymous> (C:\Users\anirudh\Documents\GitHub\Sample\server.js:1:105)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)

I am not sure what am i missing, any help would be appreciated 我不知道我错过了什么,任何帮助将不胜感激

I think a better way to go is to create a .deployment file at the root of your project that points Kudu (that's the SCM (Service Control Manager) that Azure uses to take your deployed site, figure out what it is, condition it, and make it work on IIS) to the right directory in your deployment files. 我认为更好的方法是在项目的根目录创建一个指向Kudu的.deployment文件(这是Azure用来部署站点的SCM(服务控制管理器),弄清楚它是什么,调整它,并使其在IIS上工作到部署文件中的正确目录。

It's easy. 这很简单。 Just create a new text file in the root of your directory called .deployment and put this in it... 只需在目录的根目录中创建一个名为.deployment的新文本文件,并将其放入其中......

[config]
project = server

That will tell the deployment script to look into the server folder as the root of the site. 这将告诉部署脚本将服务器文件夹作为站点的根目录查看。 Hope that works for you! 希望对你有用!

Node detection 节点检测

If you've poked around the portal interface for web sites, you may have noticed that there's a setting to enable PHP, but not one for node. 如果你在网站的门户界面上搜索过,你可能已经注意到有一个设置来启用PHP,但没有一个用于节点。 Instead, the web site looks for the presence of an app.js or server.js file in your deployment and will enable node functionality if either of those files are present. 相反,该网站会在您的部署中查找app.js或server.js文件的存在,并且如果存在这些文件中的任何一个,则将启用节点功能。

When an app.js or server.js file is detected, a web.config file will be created for the deployment that contains the configuration information required by iisnode. 检测到app.js或server.js文件时,将为包含iisnode所需配置信息的部署创建web.config文件。 Note that this file isn't added to the remote git repository, so if you do a pull from the web site's git repository you won't get this file. 请注意,此文件未添加到远程git存储库,因此如果从Web站点的git存储库执行操作,则不会获得此文件。 You can use ftp to connect to the server and find it in the wwwroot directory that contains your web site. 您可以使用ftp连接到服务器,并在包含您的网站的wwwroot目录中找到它。

Server side npm 服务器端npm

So what happens if you deploy a project that doesn't include a node_modules folder? 那么如果部署不包含node_modules文件夹的项目会发生什么? Well, if you have a package.json file that lists required modules, Windows Azure will actually run npm on your web site and try to install those modules. 好吧,如果你有一个列出所需模块的package.json文件,Windows Azure实际上会在你的网站上运行npm并尝试安装这些模块。 You can see this by creating a new express site using the express command, and then deploying that to a web site using git. 您可以通过使用express命令创建新的快速站点,然后使用git将其部署到网站来查看此信息。 You will see the expected output from git, but then you'll see output similar to the following: 您将看到git的预期输出,但随后您将看到类似于以下内容的输出:

 remote: New deployment received. remote: Updating branch 'master'. remote: Preparing deployment for commit id '3f69628e8c'. remote: Preparing files for deployment. remote: Running NPM. remote: npm http GET http://registry.npmjs.org/jade remote: npm http GET http://registry.npmjs.org/express/2.5.8 remote: npm http 200 http://registry.npmjs.org/express/2.5.8 remote: npm http GET http://registry.npmjs.org/express/-/express-2.5.8.tgz remote: npm http 200 http://registry.npmjs.org/express/-/express-2.5.8.tgz remote: npm http 200 http://registry.npmjs.org/jade remote: npm http GET http://registry.npmjs.org/jade/-/jade-0.26.1.tgz remote: npm http 200 http://registry.npmjs.org/jade/-/jade-0.26.1.tgz remote: npm WARN excluding symbolic link lib\\index.js -> jade.js remote: npm http GET http://registry.npmjs.org/mime/1.2.4 remote: npm http GET http://registry.npmjs.org/qs remote: npm http GET http://registry.npmjs.org/mkdirp/0.3.0 remote: npm http GET http://registry.npmjs.org/connect remote: npm http 200 http://registry.npmjs.org/mime/1.2.4 remote: npm http GET http://registry.npmjs.org/commander/0.5.2 remote: npm http GET http://registry.npmjs.org/mime/-/mime-1.2.4.tgz remote: npm http 200 http://registry.npmjs.org/qs remote: npm http 200 http://registry.npmjs.org/mkdirp/0.3.0 remote: npm http GET http://registry.npmjs.org/qs/-/qs-0.4.2.tgz remote: npm http GET http://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz remote: npm http 200 http://registry.npmjs.org/connect remote: npm http GET http://registry.npmjs.org/connect/-/connect-1.8.7.tgz remote: npm http 200 http://registry.npmjs.org/connect/-/connect-1.8.7.tgz remote: npm http 200 http://registry.npmjs.org/qs/-/qs-0.4.2.tgz remote: npm http 200 http://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz remote: npm http 200 http://registry.npmjs.org/mime/-/mime-1.2.4.tgz remote: npm http 200 http://registry.npmjs.org/commander/0.5.2 remote: npm http GET http://registry.npmjs.org/commander/-/commander-0.5.2.tgz remote: npm http 200 http://registry.npmjs.org/commander/-/commander-0.5.2.tgz remote: npm http GET http://registry.npmjs.org/formidable remote: npm http 200 http://registry.npmjs.org/formidable remote: npm http GET http://registry.npmjs.org/formidable/-/formidable-1.0.11.tgz remote: npm http 200 http://registry.npmjs.org/formidable/-/formidable-1.0.11.tgz remote: jade@0.26.1 ./node_modules/jade remote: ├── commander@0.5.2 remote: └── mkdirp@0.3.0 remote: remote: express@2.5.8 ./node_modules/express remote: ├── mime@1.2.4 remote: ├── qs@0.4.2 remote: ├── mkdirp@0.3.0 remote: └── connect@1.8.7 (formidable@1.0.11) remote: SetConsoleTitleW: The operation completed successfully. remote: remote: Deploying Web.config to enable Node.js activation. remote: Deployment successful. 

One thing to look out for however is modules that rely on node-gyp to compile native bits during install. 但要注意的一件事是依赖node-gyp在安装期间编译本机位的模块。 The web sites environment doesn't have things that node-gyp depends on (like Python) available, so node-gyp will fail. 网站环境没有node-gyp所依赖的东西(如Python),因此node-gyp将失败。 The workaround is to install and build the native module on a Windows system and then deploy the node_modules folder as part of the git deployment. 解决方法是在Windows系统上安装和构建本机模块,然后将node_modules文件夹部署为git部署的一部分。 This way, the compiled bits are already there. 这样,编译的位已经存在。

So once you have git repo and node is working on the website. 所以,一旦你有git repo和节点正在网站上工作。 go ahead and install loopback and then checkin your code again and you should see it action 继续安装环回,然后再次检查你的代码,你应该看到它的动作

Some general guidance on node from azure https://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac/ 关于节点的一些一般指导来自azure https://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac/

A bit old but still valid http://blogs.msdn.com/b/silverlining/archive/2012/06/14/windows-azure-websites-node-js.aspx 有点旧但仍然有效http://blogs.msdn.com/b/silverlining/archive/2012/06/14/windows-azure-websites-node-js.aspx

loopback big brother strong loop https://azure.microsoft.com/en-us/marketplace/partners/nodejsapi/node-js-api/#starter loopback big brother strong loop https://azure.microsoft.com/en-us/marketplace/partners/nodejsapi/node-js-api/#starter

Loopback server app doesn't start unless it is invoked using 除非使用调用服务器应用程序,否则它无法启动

node server/server.js 节点服务器/ server.js

IIS however invokes it from another file like so: 然而,IIS从另一个文件调用它,如下所示:

require('server/server.js') 要求( '服务器/ server.js')

which means your loopback server won't start. 这意味着您的环回服务器将无法启动。

Here's a route to fix: https://github.com/masonkmeyer/loopback-azure 这是一条修复路线: https//github.com/masonkmeyer/loopback-azure

The node . node . error is because you did unnecessary moves to the server.js file 错误是因为您对server.js文件进行了不必要的移动

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

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