3

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. 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/

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.

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. This is what i tried

  • create a server.js on the root level with contents

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"

{
  "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

Community
  • 1
  • 1
Anirudh Goel
  • 4,571
  • 19
  • 79
  • 109

3 Answers3

3

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.

It's easy. Just create a new text file in the root of your directory called .deployment and put this in it...

[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!

Jeremy Foster
  • 4,673
  • 3
  • 30
  • 49
1

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

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. 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. You can use ftp to connect to the server and find it in the wwwroot directory that contains your web site.

Server side npm

So what happens if you deploy a project that doesn't include a node_modules folder? 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. You can see this by creating a new express site using the express command, and then deploying that to a web site using git. You will see the expected output from git, but then you'll see output similar to the following:

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. The web sites environment doesn't have things that node-gyp depends on (like Python) available, so node-gyp will fail. 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. This way, the compiled bits are already there.

So once you have git repo and node is working on the website. 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/

A bit old but still valid 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

0

Loopback server app doesn't start unless it is invoked using

node server/server.js

IIS however invokes it from another file like so:

require('server/server.js')

which means your loopback server won't start.

Here's a route to fix: https://github.com/masonkmeyer/loopback-azure

The node . error is because you did unnecessary moves to the server.js file

adgang
  • 71
  • 1
  • 3