How to call a program on the server using MeteorJs? - javascript

Currently, I am building an application in Meteor. The application is based on transforming application information in an XML format. And then run a program on the XML created on the server. After that read the xml file created and bring it to a new collection.
On the other hand, personally I run on my linux machine manually:
(*) ./ myprogram -xml = myOutput.xml -out = myout.xml -timelimit = 300
myOutput.xml where is the generated file.
myout is the file generated by the program.
And timelimit is the maximum runtime of the program.
So far I have done the following:
Once all income, build a xml file with the values stored in my collections. This was performed as follows:
(1) server startup declare the use of fs
Meteor.startup (function () {
fs = Npm.require ('fs');
// ...
}
-In My methods I build the file with its corresponding information
Meteor.methods ({
transform: function () {
fs.createWriteStream wstream = var ('myOutput.xml');
...
//....operations over the collections
...
wstream.end ();
... (2) ...
},
});
All good so (2). The file is either created.
After (2), I need to make program implementation as in (*). The program depends on the xml file built and an outlet.
After contextualize my problem my questions are:
How I can run the program on the server using the file created? And
What I locate the executable folder "myprogram" to use it?

Related

readFileSync is not a function

I am relatively new to Node.js and have been looking around but cannot find a solution. I did check the require javascript file and it does not seem to have a method for "readFileSync". Perhaps I don't have a proper require file? I had a hard time finding this file, everywhere talked about it but most people did not post where to get it.
I installed Node.js and have the require.js file. My current code is like this:
fs = require(['require'], function (foo) {
//foo is now loaded.
});
console.log("\n *STARTING* \n");
// Get content from file
var contents = fs.readFileSync("sliderImages", 'utf8');
I had a bit at first getting require to work however it seems to load the require JavaScript file. I have been following guides and I am not sure why I get this error:
Uncaught TypeError: fs.readFileSync is not a function
I have tried many fixes and cannot seem to figure this one out.
Node.js does not use Require.js. Require.js was built so that you could have asynchronous module loading on the client-side (in your browser).
Node.js uses CommonJS style modules. Your code using CommonJS would look like this:
var fs = require('fs');
console.log("\n *STARTING* \n");
var contents = fs.readFileSync("sliderImages", "utf8");
If we assume you saved this in a file called main.js you would then enter this command in your console (make sure you are in the same directory as the file):
node main.js
This code will not run in the browser. Node.js runs on the server. If you want to load a JSON file on the browser side then you'll need to load it using AJAX. There are numerous resources available to show you how to do this. Be aware that you must either run your page from a server or have a special flag enabled to load in files from the file system.
This error can arise if you write
const fs = import('fs');
in a Node module
but should write
import fs from 'fs';

How to import .json file as arguments to .js file and launch it using command line?

I have a .json file stored in a local directory on my computer.
data.json
{
"foo": "bar",
"baz": "bat"
}
I want to load data.json as arguments somehow into a .js file that I also have stored locally on my computer. Then run it.
app.js
var foo = "bar",
baz = "bat"; // Somehow, I need to import these arguments from data.json
// Then do stuff with them...
app.js is a pure javascript file (without access to an HTML "wrapper" or any JS libraries like jQuery, etc.).
I would like to use one of the following to call app.js:
Command line / shell script (including but not necessarily node.js),
AppleScript (Mac OS X Yosemite v.10.10.1), or
iMacros for Firebox bookmarklet.
How can I accomplish this?
If you are using node, just require the json.
// app.js
var data = require('./data.json');
var foo = data.foo // etc
In your command line you can just run
node app.js
Of course if your data json is in a different directory, adjust the path accordingly. You could also allow it to use any json by providing it as an argument (see here)

Share constant from node.js sever to js files in frontend app

I have a node.js server and a package.json file. In that file I set some variables. For exemple "version" : 3.0. I can access those variables very easily in the node.js server. (Is there a way to get version from package.json in nodejs code?)
var pjson = require('./package.json');
console.log(pjson.version);`
But how can I pass it to my js app working on the frontend? Can I create a constant.js file that is created went I start the server (only ones).
I do not want to pass the variable as an argument every time I render a page.
I use the ejs to render my pages.
Thank you for your help.
You could set a cookie in the initial response.
response.setHeader("Set-Cookie", ["version=3.0"])

Requiring/running JavaScript outside of node-webkit application

Suppose I have the following app structure:
outer-folder/
├── my-app/
└── settings.js
where my-app/ is either the unbuilt directory that contains package.json or the packaged application my-app.exe or my-app.app.
I cannot package settings.js with the rest of my app, because I want this file to be editable by users, and loaded upon startup to configure my app. Moreover, I plan to allow settings.js to reside anywhere (for example, in the user's home directory) and be loadable from there.
This works fine when the app is unbuilt. I just have a relative path to the file and require() it like usual. But when the app is built (with grunt-node-webkit-builder if that makes a difference) this fails and I get the dreaded "Cannot find module" error.
I've searched the node-webkit wiki but can't find anything about this issue, am I missing something? What is the best way to load and run an external JavaScript file, like one would do with require(), from a packaged node-webkit app?
I suggest you to use the application data path.
See the documentation here
An exemple
var gui = require('nw.gui');
var path = require('path');
var yaml = require('js-yaml');
var fs = require('fs');
var confPath = path.join(gui.App.dataPath, 'conf', "dev-conf.yml");
try {
conf = yaml.load(fs.readFileSync(confPath, 'utf-8'));
} catch (err) {
throw new Error("Cannot read or parse configuration file '"+confPath+"': "+err);
}
It's a good pratice to separate code and configuration, and App.dataPath aims at the application specific folder in user's application data (different for each OS).
I generally use an installer to copy my configuration file into it.
Next tip: I prefer using YAML instead of JSON for my configuration or settings, because it allows you to insert comments inside the file.

How to run user-submitted modules securely in a node.js?

We are planning to develop a business oriented application platform on node.js + express. And we like to allow users to run their own native node.js modules (set of files js, css, html), so generally it should be like portal and portles/servlets. Users should have ability to install modules on server side with its client part and these modules should interact with platform and other modules throw some api. So needed to isolate these modules from direct access to the system files and database, but they should have access to their own files and database. Please help me what direction should we dig to make it secure.
I have checked information about: sandbox in vm and child process.
I tried:
// Main file:
var util = require('util'),
vm = require('vm'),
fs = require('fs'),
sandbox = {
animal: 'cat',
count: 2,
require: require // I pass it to make possible for the module to
// include some additional files
// but it opens access for all system files
};
var context = vm.createContext(sandbox);
fs.readFile('./user_modules/index.js', 'utf8', function (err, data) {
vm.runInNewContext(data, context);
console.log(util.inspect(context));
});
//** User Module
// user_modules/index.js
var fs = require('fs');
count++;
animal = 'Dog';
fs.readFile('README.md', 'utf8', function (err, data) {
animal = 'Fox';
});
I passed REQUIRE object to module to make possible to include some additional files but it opens access for all system files, is it possible to tell VM or child process to work only with specific folders? Currently I have no idea how to work with database, but I think when user will install his module the platform should copy all files and create a db scheme for the user then when the module will launch I need pass only object which connected to the user dbscheme .
Please help me, I’m really new with nodes, any suggestions how to solve my issue?
Thanks in advance
One thing you could do is to create a shim function around require that does whatever validation you want, and then calls the system's require function. You can then pass that in to the sandbox as a replacement for "require".
I'm not sure of all the changes that would be necessary to make a "secure" sandbox for node.js. To some extent, that's going to depend on what the user-submitted modules need to do.
One way to help ensure that the user modules can't interfere with your code would be to run them in their own process. On a unix system, you can use chroot to create an isolated filesystem for the process to run in, and then communicate with the process over a stdio pipe, or a socket.

Categories

Resources