384 CHAPTER 8 Websites and services
“formidable”: “1.x”,
“express”: “3.0.0”
}
}
- Install the dependent packages by typing the following command.
npm install - Open Visual Studio Express 2012 for Web. Click File and choose Open Web Site; select
the ContosoWeb folder. - In the Contoso website folder, create an index.js file and add a reference to the express
and formidable packages as follows.
var express = require('express');
var app = express();
var formidable = require('formidable'); - Add a statement to map static requests to the WebSolution/WebCalculator folder as
follows.
app.use(express.static(__dirname + '/WebCalculatorSolution/WebCalculator')); - Add code to redirect the user to the default.html page if the URL does not include a
file name, as follows.
app.get('/', function (request, response) {
response.redirect('default.html');
}); - Add code to listen on port 8080 and log a message to the console stating this.
The index.js file should look like the following.
var express = require('express');
var app = express();
var formidable = require('formidable');
app.use(express.static(__dirname + '/WebCalculatorSolution/WebCalculator'));
app.get('/', function (request, response) {
response.redirect('default.html');
});
var port = 8080;
app.listen(port);
console.log('Listening on port: ' + port);
- Test your work by running the website and using the following command in the com-
mand prompt window.
node index