I picked a tutorial from the internet with these code:
// calc.js
const add = (x,y) => x + y;
const multiply = (x, y) => x * y;
module.exports = {
add,
multiply
};
and
// index.js
const cal = require('./cal');
console.log(calc.add(2, 3));
console.log(calc.multiply(2, 3));
They are good example but how do I call the functions from browser something like this:
// index.js
const http = require('http');
const fs = require('fs');
const calc = require('./calc');
var handleRequest = (request, response) => {
response.writeHead(200, {
'Content-Type': 'text/html'
});
fs.readFile('./index.html', null, function (error, data) {
if (error) {
response.writeHead(404);
respone.write('Whoops! File not found!');
} else {
response.write(data);
}
response.end();
});
};
http.createServer(handleRequest).listen(8000);
and
// index.html
< script>
console.log(calc.add(2, 3));
console.log(calc.multiply(2, 3));
</ script>
Can this be possible and how? Noted that Content-Type isn’t the issue and the reason I want to do this is because I want to use some require() functions.
If this isn’t possible, what is the way to do it.
Thank you,