1. Aug 9th, 2011

    Using Pow with your Node.js project

    Pow is great for two reasons: it lets me access the development server on port 80, and it lets me switch between multiple projects, each with its own meaningful host name.

    I just wish I could be running my Node.js projects on port 80 with nicely mapped URLs.

    Well, this little hack gets me all the way there with no sweat.

    Step 1: Create a config.ru in the root of your project:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    require "net/http"
     
    class ProxyApp
      def call(env)
        begin
          request = Rack::Request.new(env)
          headers = {}
          env.each do |key, value|
            if key =~ /^http_(.*)/i
              headers[$1] = value
            end
          end
          http = Net::HTTP.new("localhost", 8080)
          http.start do |http|
            response = http.send_request(request.request_method, request.fullpath, request.body.read, headers)
            [response.code, response.to_hash, [response.body]]
          end
        rescue Errno::ECONNREFUSED
          [500, {}, ["Server is down, try $ npm start"]]
        end
      end
    end
    run ProxyApp.new

    Step 2: Symlink your project into the Pow directory (just as you would with a Ruby project).

    Step 3: Start the Node server.

    I prefer to use npm start because it’s an easy convention to remember, it defaults to running node server.js but you can customize what it does in package.json.

    If you’re more adventurous, you could run a server written in CoffeeScript using node-supervisor, which will take care of reloading the server whenever you make a code change. Here’s the relevant package.json entry:

    1
    2
    3
    
      "scripts": {
        "start": "supervisor server.coffee"
      },

    Enjoy.

    Update Fixed config.ru to support POST requests and pass headers to the proxy.

    1. Aug 7th, 2011

      Link dump for August 7th | The Queue Incorporated

      [...] Using Pow with your Node.js project /by @assaf – pow for node.js dev [...]

    2. Aug 7th, 2011

      links for 2011-08-07 « Bloggitation

      [...] Using Pow with your Node.js project (tags: node.js rack pow sysadmin) [...]

    3. Sep 16th, 2011

      Florian Klotz

      thanks! worked like a charm

    4. Oct 24th, 2011

      Andrew

      http://cl.ly/1S3p1L0i3K3C1T300t0e
      this is what I get. Didn’t work for me.
      Don’t know what to do

    Your comment, here ⇓