362 CHAPTER 8 Websites and services
if (request.method.toLowerCase() == 'post') {
// parse form data
var form = new formidable.IncomingForm();
form.parse(request, function (err, fields) {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('Hello ' + fields.userName + '!<br />');
response.end('Have a POST great day!');
console.log('Handled request from ' + fields.userName);
});
}
});
app.get('/SubmitHello', function (request, response) {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('Hello ' + request.query.userName + '!<br />');
response.end('Have a great day!');
console.log('Handled request from ' + request.query.userName);
});
var port = 8080;
app.listen(port);
console.log('Listening on port: ' + port);
Run the application and then enter the following in the browser URL.
http://localhost:8080/Forms/HelloPost.html
Enter a name in the text box and click Submit. You should see the result displayed in the
browser window, as shown in Figure 8-11.
FIGURE 8-11 he posted form data processed and displayedT
There is no QueryString, and the URL clearly shows that the posted data was sent to the
SubmitHelloPost. In earlier examples, the GET method was used to show you how it works.
Remember that you should always send data back to the server by using the POST method.