Getting Started With Node.js in Windows 7

nodejsSeveral years ago I built a very high performance event-based asynchronous TCP server using .NET sockets. Looking back on this project I realize it was literally Node.js in disguise (minus the js part), so I decided to revisit and release it as open source on Github. Node.js probably did not exist when I was building proof of concept chat apps using this server as the framework, and there might have been a good portion of fanfare to be had if I released the code sooner. I wasn’t too big about open source back then, and it’s not the first time I’ve unknowingly had “big tech” collecting dust in my project bin. Boohoo, am I right? With a renewed interest in this project, I decided to do some benchmarks and see how well the .NET server compares to Apache or Node.js.

In this article I will show how to get started with Node.js in Windows 7. In a follow up article I will include the benchmark results and tips on how to set that up.

Installing Node.js and Creating a Server

Problem #1 – The MSI installer worked fine, but afterwards I couldn’t get npm to work out of the box – every attempt at using it would result in an ENOENT error concerning the path C:\Users\<username>\AppData\Roaming\npm.

Problem #2 – I wanted to get a quick server running with express, so I pulled up a few tutorials from the web and tried them out. None of the tutorials worked because apparently express.createServer() is a deprecated function that no longer works.

Here’s how I got the Node.js server up and running in Windows 7 with minimal fuss:

  1. Install Node.js using the MSI installer.
  2. Create the missing “npm” folder which solves Problem #1 above. For example: C:\Users\<username>\AppData\Roaming\npm
  3. Create a new folder somewhere for your website/app.
  4. In the new folder, create a package.json file using Notepad, with the following:
    {
      "name": "hello"
    , "version": "0.0.1"
    , "dependencies": {
        "express": "latest"
      }
    }
    
  5. In the new folder, also create an app.js file using Notepad, with the following:
    var app = require("express")();
    app.get('/', function(req, res) {res.send('hello world');});
    app.listen(9999)
    

    This solves Problem #2 by making sure we don’t use the deprecated method app.createServer() on line 1.

  6. Open a command prompt and navigate to the new folder you created in Step 3.
  7. Run the “npm install” command. At this point, Node will automatically install the dependencies listed in the package.json file, which in this case is simply the latest version of express.
  8. Run the “node app.js” command. At this point, Node will start an HTTP server that listens on port 9999 and you should be able to access it via a web browser with http://localhost:9999/. You should see “hello world” on a white page.

Related

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *