title: How to post to local machine - Query, node.exe
body: <p>I know there are a few questions/answers here already but none of them work for me.  I would like to post on my local machine a JQuery page but I can't get it working.</p>
  
  <p>Here is an example, it just posts some results to a page on a server that doesn't exist but I don't know how to get it working on my local machine.   </p>
  
  <pre><code>    &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Example&lt;/title&gt;
      &lt;script src="jquery-1.7.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="jquery.tmpl.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" type="text/css" href="styles.css"/&gt;
      &lt;script type="text/javascript"&gt;
          $(document).ready(function() {
  
              var data = [
                 { name: "Astor", product: "astor", stocklevel: "10", price: "2.99"},
                 { name: "Daffodil", product: "daffodil", stocklevel: "12", price: "1.99"},
                 { name: "Rose", product: "rose", stocklevel: "2", price: "4.99"},
                 { name: "Peony", product: "peony", stocklevel: "0", price: "1.50"},
                 { name: "Primula", product: "primula", stocklevel: "1", price: "3.12"},
                 { name: "Snowdrop", product: "snowdrop", stocklevel: "15", price: "0.99"},
              ];
  
              var templResult = $('#flowerTmpl').tmpl(data);
              templResult.slice(0, 3).appendTo('#row1');
              templResult.slice(3).appendTo("#row2");
  
          });
      &lt;/script&gt;
      &lt;script id="flowerTmpl" type="text/x-jquery-tmpl"&gt;
          &lt;div class="dcell"&gt;    
              &lt;img src="${product}.png"/&gt;
              &lt;label for="${product}"&gt;${name}: &lt;/label&gt;
              &lt;input name="${product}" value="0" required /&gt;
          &lt;/div&gt;
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;Jacqui's Flower Shop&lt;/h1&gt;
      &lt;form method="post" action="http://node.jacquisflowershop.com/order"&gt;
          &lt;div id="oblock"&gt;        
              &lt;div class="dtable"&gt;
                  &lt;div id="row1" class="drow"&gt;
                  &lt;/div&gt;
                  &lt;div id="row2"class="drow"&gt;
                  &lt;/div&gt;            
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div id="buttonDiv"&gt;&lt;button type="submit"&gt;Place Order&lt;/button&gt;&lt;/div&gt;                    
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I was running node.exe on my machine and it was listening to a port number - basically listening to what what posted.  </p>
  
  <p>Is there any way I could post on my local machine, I have IIS installed but didn't get anything working.</p>
  
  <p><strong>edit</strong></p>
  
  <p>There is a script that goes with this called formserver.js.  I would run it with node.exe formserver.js.  I don't know how to post to local machine ?</p>
  
  <pre><code>    var http = require('http');
  var url = require('url');
  var querystring = require('querystring');
  
  http.createServer(function (req, res) {
      console.log("Request: " + req.method + " to " + req.url);
  
      if (req.method == 'OPTIONS') {
          res.writeHead(200, "OK", {
              "Access-Control-Allow-Headers": "Content-Type",
              "Access-Control-Allow-Methods": "*",
              "Access-Control-Allow-Origin": "*"
              });
          res.end();
  
      } else if (req.method == 'POST') {
          var dataObj = new Object();
          var contentType = req.headers["content-type"];
          var fullBody = '';
  
          if (contentType) {
              if (contentType.indexOf("application/x-www-form-urlencoded") &gt; -1) {
                  req.on('data', function(chunk) { fullBody += chunk.toString();});
                  req.on('end', function() {            
                      var dBody = querystring.parse(fullBody);
                      writeResponse(req, res, dBody,
                          url.parse(req.url, true).query["callback"])
                  });
              } else {
                  req.on('data', function(chunk) { fullBody += chunk.toString();});
                  req.on('end', function() {
                      dataObj = JSON.parse(fullBody);
                      var dprops = new Object();
                      for (var i = 0; i &lt; dataObj.length; i++) {
                          dprops[dataObj[i].name] = dataObj[i].value;
                      }
                      writeResponse(req, res, dprops);
                  });
              }
          }
      } else if (req.method == "GET") {
          var data = url.parse(req.url, true).query;            
          writeResponse(req, res, data, data["callback"])
      }
  
  }).listen(8080);
  console.log("Ready on port 8080");  
  
  var flowerData = {
      astor: { price: 2.99, stock: 10, plural: "Astors"},
      daffodil: {price: 1.99, stock: 10, plural: "Daffodils"},
      rose: {price: 4.99, stock: 2, plural: "Roses"},
      peony: {price: 1.50, stock: 3, plural: "Peonies"},
      primula: {price: 3.12, stock: 20, plural: "Primulas"},
      snowdrop: {price: 0.99, stock: 5, plural: "Snowdrops"},
      carnation: {price: 0.50, stock: 1, plural: "Carnations"},
      lily: {price: 1.20, stock: 2, plural: "Lillies"},
      orchid: {price: 10.99, stock: 5, plural: "Orchids"}
  }
  
  function writeResponse(req, res, data, jsonp) {
      var jsonData;
      if (req.url == "/stockcheck") {
          for (flower in data) {
              if (flowerData[flower].stock &gt;= data[flower]) {
                  jsonData = true;
              } else {
                  jsonData = "We only have " + flowerData[flower].stock + " "
                      + flowerData[flower].plural + " in stock";
              }
              break;
          }
          jsonData = JSON.stringify(jsonData);
      } else {       
          var totalCount = 0;
          var totalPrice = 0;
          for (item in data) {
              if(item != "_" &amp;&amp; data[item] &gt; 0) {
                  var itemNum = Number(data[item])
                  totalCount += itemNum;
                  totalPrice += (itemNum * flowerData[item].price);
              } else {
                  delete data[item];
              }
          }
          data.totalItems = totalCount;
          data.totalPrice = totalPrice.toFixed(2);
  
          jsonData = JSON.stringify(data);
          if (jsonp) {
              jsonData = jsonp + "(" + jsonData + ")";
          }
      }
      res.writeHead(200, "OK", {
          "Content-Type": jsonp ? "text/javascript" : "application/json",
          "Access-Control-Allow-Origin": "*"});    
      res.write(jsonData);
      res.end();
  } 
  </code></pre>