Import Connect Module

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

// import connect module

var connect = require('connect');


var url = require('url');

// creating app object


var app = connect();

// configure the port and start listening to requests


app.listen(3000);
console.log('Server running at http://localhost:3000');

// middleware function to handle requests


function calculate(req, res, method, x, y) {
let query = url.parse(req.url, true);
let method, x, y = query;

let x = parseFloat(x);
let y = parseFloat(y);

let result;

switch (method) {
case 'add':
result = x + y;
break;
case 'subtract':
result = x - y;
break;
case 'multiply':
result = x * y;
break;
case 'divide':
result = x / y;
break;
default:
res.end('Invalid method parameter');
return;
}
res.end(`${x} ${method} ${y} = ${result}`);
}
app.use(calculate);

You might also like