Reputation:
I am new to node.js and a relatively new to coding. I have written node.js code that I need to run on a android device. I chose node.js because of my javascript experience. It is using the net library to communicate with a device. I have done a lot of googling and I am now going around in circles with regards to the way I can do this. I have read about PhoneGap etc. I need to be able to run node.js locally. My question is simple. Can this be done? Any help would be appreciated.
If node.js is not the correct approach in your opinion then any advice on cummunicating with devices using a android tablet will be appreciated.
Thank you in advance.
Kind Regards, Jason.
Upvotes: 1
Views: 760
Reputation: 1037
Yes, It is possible to run node.js on android. Since Node.js requires V8 javascript engine whihch is not available for android device. Jxcore is the alternative to that.
I am able to run JxCore with Cordova. JxCore is almost similar to Node.js Find the below link for reference : https://github.com/jxcore/jxcore-cordova
Below given is the link for working sample of Angular cordova application : https://github.com/karaxuna/jxcore-tutorial-angular
I am able to run this sample and created express server inside the android device itself.
For running express server in the above sample : Just change the jxcore/app.js to
var express = require('express');
var app = express();
app. post('/sample', function (req, res) {
var headers = {};
// set header to handle the CORS
headers['Access-Control-Allow-Origin'] = '*';
// headers['Access-Control-Allow-Origin'] = 'http://localhost:?';
headers['Access-Control-Allow-Headers'] = 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With';
headers['Access-Contrl-Allow-Methods'] = 'PUT, POST, GET, DELETE, OPTIONS';
headers["Access-Control-Max-Age"] = '86400';
headers["Content-Type"] = 'application/json';
res.writeHead(200, headers);
setTimeout(() => {
res.write('{"message" : "hello"}');
res.end();
}, 1000);
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Make sure, node_modules are also copied in the application.
Check the server working from the android browser.
Upvotes: 1