Tuesday, September 15, 2015

First Node.js App in Azure

I read Ted Neward's article about deploying a simple Node.js app to Microsoft Azure and wanted to give it a try.

The steps mostly worked, except for a few issues. Below are the steps that he lays out. I am assuming that you have clicked the link and read the article. He does a much better job explaining everything. In this post, I wanted to note the changes necessary with the steps.

  1. In a local directory, create the following Javascript file. This file creates a basic web server using Node.js.
           var http = require('http');
           var port = process.env.PORT || 3000;
           http.createServer(function(req, res) {
             res.writeHead(200, { 'Content-Type': 'text/plain' });
             res.end('Hello World');
           }).listen(port);
         
    In the article, this script is named "helloHTTP.js." For the file to work in Azure, it needs to be named "server.js".
  2. Test the script using Node.js
    1. Run the script using Node.js:
      node server.js
    2. Open the URL http://localhost:3000 in your browser.
    3. You should see the string "Hello World".
  3. Install the azure-cli tools:
    npm install -g azure-cli
  4. Access your Azure account:
    azure account download
  5. Import the file that was just downloaded:
    azure account import 
  6. Create the site with git:
    azure site create --git
    In the article, the argument for the git option only showed a single dash. Also, this step will prompt you for a site user name and region. The name you choose must be unique across Azure. Pay attention to the messages too. It will give you the full URL necessary for accessing the site once it is created.
  7. Add the script file:
    git add server.js
  8. Commit the file:
    git commit -m "Initial site creation."
  9. Push the site to Azure:
    git push azure master
At this point, you should be able to navigate to the site name created above, and see "Hello World" generated by a Node.js script in Azure.

No comments:

Post a Comment