Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

Preparing the Node.js Server > Preparing the Node.js Server - Pg. 359

CHAPTER 13 WORKING WITH FORMS Listing 13-1. The formserver.js Node.js Script var http = require('http'); var querystring = require('querystring'); http.createServer(function (req, res) { console.log("[200 OK] " + req.method + " to " + req.url); 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") > -1) { req.on('data', function(chunk) { fullBody += chunk.toString();}); req.on('end', function() { res.writeHead(200, "OK", {'Content-Type': 'text/html'}); res.write('<html><head><title>Post data</title></head><body>'); res.write('<style>th, td {text-align:left; padding:5px; color:black}\n'); res.write('th {background-color:grey; color:white; min-width:10em}\n'); res.write('td {background-color:lightgrey}\n'); res.write('caption {font-weight:bold}</style>'); res.write('<table border="1"><caption>Form Data</caption>'); res.write('<tr><th>Name</th><th>Value</th>'); var dBody = querystring.parse(fullBody);