376 CHAPTER 8 Websites and services
var y = $('#y').val();
var data = { "x": x, "y": y };
$.post('/subtraction', data, function (data) {
$('#result').html(data.result);
}, 'json');
}
function multiplyNumbers() {
var x = $('#x').val();
var y = $('#y').val();
var data = { "x": x, "y": y };
$.ajax({
url: '/multiply',
data: data,
type: 'PUT',
dataType: 'json',
cache: false,
success: function (data) {
$('#result').html(data.result);
}
});
}
function divideNumbers() {
var x = $('#x').val();
var y = $('#y').val();
var data = { "x": x, "y": y };
$.ajax({
url: '/divide',
data: data,
type: 'DELETE',
dataType: 'json',
cache: false,
success: function (data) {
$('#result').html(data.result);
}
});
}
The following is the completed app.js file that shows the routing for the subtraction, multi-
ply, and divide requests.
var express = require('express');
var app = express();
var formidable = require('formidable');
var math = require('math_example');
app.use(express.static(__dirname + '/public'));
app.get('/addition', function (request, response) {
var x = Number(request.query.x),
y = Number(request.query.y),
result = math.addition(x, y);
response.writeHead(200, { 'Content-Type': 'application/json' });