Lesson 1: Getting started with Node.js CHAPTER 8 361
In the command prompt window, execute the following npm command to retrieve infor-
mation about the formidable package.
npm info formidable
At the time of this writing, the latest version is 1.0.10, but you can also use a wild card to
specify the version in the package.json file in which you must specify this dependency. The
following is the modified package.json file.
{
"name": "HelloExpress",
"version": "0.0.0",
"description": "A simple Web site",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"author": "Glenn Johnson",
"license": "BSD",
"private": true,
"dependencies": {
“formidable”: “1.x”,
"express": "3.0.0"
}
}
The addition of formidable version 1.x retrieves the latest release of version 1.
In the command prompt window, install the formidable package by entering the following
npm command.
npm install
This command reads the package.json file and installs the dependent packages. You now
have the formidable package installed locally, so update the app.js file to process post-
back data. At the top of the app.js file, add the following code to reference the formidable
package.
var formidable = require('formidable');
Next, process the posted data. Instead of adding an app.get function call, add an app.
post function call. In the call, you must create an instance of the formidable package’s
IncomingForm object and assign it to a form variable. You then must use the form variable
fields and display a message to the user. The completed app.js file with the app.post function
should look like the following.
var express = require('express');
var app = express();
var formidable = require('formidable');
app.use('/forms', express.static(__dirname + '/public'));
app.post('/SubmitHelloPost', function (request, response) {