/
server.js
79 lines (71 loc) · 1.91 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* This example demonstrates how to run math.js in a child process with limited
* execution time.
*
* Prerequisites:
*
* npm install express workerpool
*
* Start the server:
*
* node ./server.js
*
* Make a request to the server:
*
* GET http://localhost:8080/mathjs?expr=sqrt(16)
*
* Note that the query parameter `expr` should be properly url encoded.
*/
try {
var express = require('express');
var workerpool = require('workerpool');
}
catch (err) {
console.log('Error: To run this example, install express and workerpool first via:\n\n' +
' npm install express workerpool\n');
process.exit();
}
var app = express();
var pool = workerpool.pool(__dirname + '/math_worker.js');
var TIMEOUT = 10000; // milliseconds
/**
* GET /mathjs?expr=...
*/
app.get('/mathjs', function (req, res) {
var expr = req.query.expr;
if (expr === undefined) {
return res.status(400).send('Error: Required query parameter "expr" missing in url.');
}
pool.exec('evaluate', [expr])
.timeout(TIMEOUT)
.then(function (result) {
res.send(result);
})
.catch(function (err) {
res.status(400).send(formatError(err));
});
});
/**
* Format error messages as string
* @param {Error} err
* @return {String} message
*/
function formatError (err) {
if (err instanceof workerpool.Promise.TimeoutError) {
return 'TimeoutError: Evaluation exceeded maximum duration of ' + TIMEOUT / 1000 + ' seconds';
}
else {
return err.toString();
}
}
// handle uncaught exceptions so the application cannot crash
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
console.trace();
});
// start the server
var PORT = process.env.PORT || 8080;
app.listen(PORT, function() {
console.log('Listening at http://localhost:' + PORT);
console.log('Example request:\n GET http://localhost:' + PORT + '/mathjs?expr=sqrt(16)');
});