node-julia
node-julia copied to clipboard
Issues in Wrapping Node-Julia as REST API
I can now install node-julia with your help. Now I want to expose Node-Julia functions as RESET API but I find issues when calling julia.eval(). any pointer? thanks.
Sample Codes I've modified your readme.md example by wrapping node express.js but I don't know how to return julia.eval() into a variable. Let's say the following program is called 'restapi01.js' and I can run with "node restapi01.js".
var express = require('express');
var app = express();
var fs = require("fs");
var julia = require('node-julia');
app.get('/juliaExample01', function(req, res) {
var aValue = julia.eval('e^10',function(err,res) {
if(!err) console.log('exp(10) = ',res)
});
// simple prevention of premature exit
setTimeout(function(){ console.log('done.'); },1000);
res.end (aValue);
});
var server = app.listen(8080, function () {
//var host = server.address().address
var host = "127.0.0.1";
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
Issue
- How to code res.json() so that it can return Julia.eval() value as JSON data in http response I tried a couple of options but they don't work for me, e.g.
Example 1 (within a function)
res.setHeader('Content-Type', 'application/json');
res.json({ b: 1 });
// or res.json(a);
Example 2
var a = julia.eval('[2 1; 1 1]');
console.log (a);
res.json(a);
- When assign a variable to a node-julia function, it does not return a value. It's probably due to the reason that i don't know how to assign return value from a julia afunction.
[root@641a84c84b98 /]# curl -I -X GET http://localhost:8080/juliaExample01
HTTP/1.1 200 OK
X-Powered-By: Express
Date: Wed, 24 Feb 2016 18:51:56 GMT
Connection: keep-alive
Transfer-Encoding: chunked
I'd expect this REST API should return a JSON response that contains the Julia.eval() result.
There are similar Julia packages (e.g. JuliaWebAPI, Morsel) but they don't work well. JuliaWebAPI has issue with the dependency HttpParser. it can't parse integer and throws NULL exception. There are other runtime errors too which are not covered by their unit tests. Morsel is retired. Node-Julia is appealing. thanks
Examples? Sure. I think I can put together some examples. For right now, I think you're already there, I am getting the same thing nearly, but maybe just wget instead? Or maybe it's simply a matter of wrapping an array with a Javascript object. Here are some simple steps.
make skeleton
use express-generator, that auto-generates a simple express based skeleton app.
npm -g install express-generator
then
express example1
which will create a directory with a routes directory,
modify source
- routes/nj.js
from there (for example) you can create a file nj.js in routes:
var julia = require('node-julia');
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
var a = julia.eval('[2 1; 1 1]');
res.json({ julia_result:a });
});
module.exports = router;
- app.js
then in app.js add the following lines, I've put both together for brevity, but put the require with the other require statements and the use with the other use statements. it will be obvious.
var nj = require('./routes/nj');
app.use('/nj', nj);
- package.json
add node-julia to the package.json file; for you right now since you're using an unreleased feature, use the full git repo name
"node-julia": "git://github.com/waTeim/node-julia.git",
install then run
and then install to run it type in the following in the example1 directory:
npm install
./bin/www
test
and then, you're using curl, I'm using wget. Same thing basically,
wget http://localhost:3000/nj
followed up with
cat nj
{"julia_result":[{"0":2,"1":1},{"0":1,"1":1}]}