forked from colbygk/mathslax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
79 lines (74 loc) · 2.68 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
var Express = require('express');
var BodyParser = require('body-parser');
var Jade = require('jade');
var Typeset = require('./typeset.js');
var util = require('util');
var SERVER = process.env.SERVER || '127.0.0.1';
var PORT = process.env.PORT || '8080';
// Install the routes.
var router = Express.Router();
router.get('/', function(req, res) {
res.json(['Hello', 'World', {underDevelopment: true}]);
});
router.post('/typeset', function(req, res) {
var cd = new Date();
var requestString = req.body.text;
var bpr = 'math\\!';
console.log(cd + ":" + requestString);
console.log( " going to send "+bpr );
var typesetPromise = Typeset.typeset(requestString,bpr);
if (typesetPromise === null) {
res.send('no text found to typeset');
res.end(); // Empty 200 response -- no text was found to typeset.
return;
}
var promiseSuccess = function(mathObjects) {
var locals = {'mathObjects': mathObjects,
'serverAddress': util.format('http://%s:%s/', SERVER, PORT)};
var htmlResult = Jade.renderFile('./views/slack-response.jade', locals);
res.json({'text' : htmlResult});
res.end();
};
var promiseError = function(error) {
console.log('Error in typesetting:');
console.log(error);
res.end(); // Empty 200 response.
};
typesetPromise.then(promiseSuccess, promiseError);
});
router.post('/slashtypeset', function(req, res) {
var cd = new Date();
var requestString = req.body.text;
var typesetPromise = Typeset.typeset(requestString,'');
if (typesetPromise === null) {
res.send('no text found to typeset');
res.end(); // Empty 200 response -- no text was found to typeset.
return;
}
var promiseSuccess = function(mathObjects) {
var locals = {'mathObjects': mathObjects,
'serverAddress': util.format('http://%s:%s/', SERVER, PORT)};
var htmlResult = Jade.renderFile('./views/slack-slash-response.jade', locals);
res.send(htmlResult);
res.end();
};
var promiseError = function(error) {
console.log('Error in typesetting:');
console.log(error);
res.end(); // Empty 200 response.
};
typesetPromise.then(promiseSuccess, promiseError);
});
// Start the server.
var app = Express();
app.use(BodyParser.urlencoded({extended: true}));
app.use(BodyParser.json());
app.use('/static', Express.static('static'));
app.use('/', router);
app.listen(PORT);
console.log("Mathslax is listening at http://%s:%s/", SERVER, PORT);
console.log("Make a test request with something like:");
console.log("curl -v -X POST '%s:%d/typeset' --data " +
"'{\"text\": \"math! f(x) = x^2/sin(x) * E_0\"}' " +
"-H \"Content-Type: application/json\"", SERVER, PORT);
console.log('___________\n');