Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

384 CHAPTER 8 Websites and services


“formidable”: “1.x”,
“express”: “3.0.0”
}
}


  1. Install the dependent packages by typing the following command.
    npm install

  2. Open Visual Studio Express 2012 for Web. Click File and choose Open Web Site; select
    the ContosoWeb folder.

  3. 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');

  4. Add a statement to map static requests to the WebSolution/WebCalculator folder as
    follows.
    app.use(express.static(__dirname + '/WebCalculatorSolution/WebCalculator'));

  5. 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');
    });

  6. 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);


  1. Test your work by running the website and using the following command in the com-
    mand prompt window.
    node index

Free download pdf