possible to run createDocumentFragment in node.js? - node.js

I'm trying to run some javascript code (originally developed for browser) in node.js environment.
I use createDocumentFragment in order to minimize the node access.
(Obviously it is to create Dom elements in the document body)
I can run $.append using cheerio as $ in node.js.
Is there a way to run createDocumentFragment in node.js?

No because node.js initializes it in jscript not in DOM

Related

Shared NodeJS and browser libraries? DOMParser => dom-parser and createNodeIterator => dom-node-iterator

Now that I've realized that executing a function like
let parser = new DOMParser();
works only when it's executed with a browser, but not with a server like Express/Node, then, what are the options?
I've read many posts with NodeJS alternatives to DOMParser, but then there're the libraries
https://www.npmjs.com/package/dom-parser
https://www.npmjs.com/package/dom-node-iterator
Are these ones valid alternatives? Do I install them and adapt the method calls depending on whether it is the browser or Node the one that uses them?
I'm asking because looking at posts like this one HTML-parser on Node.js it doesn't seem so simple

How to disable users from being able to view app through regular browsers in express / electron app

I have an express server that serves my angular front end at http://localhost:9000
I'm using electron as a desktop client.
I want to force users to view the application through electron and only through electron. I don't want users to have the ability to browser the application through any other browser.
Is there any way to disable the ability to access the app through a regular browser?
I've attempted to find information regarding this but have come up short.
EDIT: This can only be done on the client side
You can check if the window.process object exists.
if (window.process && window.process !== undefined) {
// Likely electron
}
I don't know if it's related, but is it possible for you not to use localhost? I found that after building angular parts (with ng run build) and referencing them in electron's main.js there was no need for local server to be running (but so far I only stuffed angular's quickstart into electron shell)

Node.js install cloudant module

Hi I am making a Cordova app for iOS and Android. I am trying to use the cloudant module https://github.com/cloudant/nodejs-cloudant to access my cloudant couchDB, but for the life of me, I cannot figure out the issue I am having while installing it. Using linux, I executed npm install --save cloudant at the root of my Cordova project folder. When using var Cloudant = cordova.require('cloudant'); in my code, I get the error Uncaught module cloudant not found from within my cordova.js file as it tries to load the module. Also when I run $ node -e 'require("cloudant"); console.log("Cloudant works");' in my shell, I receive a terminal output stating "Cloudant works". I cannot seem to figure out what the issue is, and have tried many different things. Any help is extremely appreciated, as I am at my witts end at this point. Thanks.
As #Parth says, 'nodejs-cloudant' is a Node.js/npm module. To access Cloudant from within your Cordova application you will need client side code to interact with Cloudant's HTTP API.
You could use jQuery's Ajax functions to make HTTP requests to Cloudant or you can use PouchDB as a client-side library e.g:
<script src="pouchdb-3.4.0.min.js"></script>
<script>
var db = new PouchDB("https://myusername:mypassword#myaccount.cloudant.com/mydb";
</script>
Both solutions require you to enable CORS on your Cloudant account.
'nodejs-cloudant' is an npm module and not a cordova module. you just need to write
var Cloudant = require('cloudant'); That should solve the problem.
Try with this:
var Cloudant = require('cloudant');
Instead of importing cloudant directly, try using cradle or nano for accessing cloudant
'DB.const nano = require('nano')('//cloudant api link//');'
This works
https://www.npmjs.com/package/nano#dbinsertdoc-params-callback
https://www.npmjs.com/package/cradle

Prevent RequireJS from Caching Required Scripts on Nodejs

I am using something like airbnb's rendr, i.e. my own implementation of sharing backbone code between client and server, to build full HTML on the server using the same Backbone models, views, collections and templates I am using on the client. My files/modules are defined as requirejs modules, that way I can share them easily on the client.
In development mode, I want requirejs to refetch/reload any modules from disc when I refresh my browser (without restarting the server), so I get my server rendering uses the newest templates and javascript to finally serve me the newest HTML.
when using requirejs on the server with nodejs, the trick of appending bust parameters to the urlArgs like the following doesn't work, i.e. the server doesn't reload/refetch any modules from disc
urlArgs: "bust=v2"
I wonder, if reloading/refetching requirejs modules from disc-space without restarting the server is possible in node? specifically, that would be very useful for the require-text plugin for template. additionally, it would be nice to apply reloading only to a restricted set of modules.
I've never had to do this, but a quick search shows a few options, including automating the server restart:
nodemon
node-supervisor
You may also be able to do something similarly creative to this:
delete require.cache['/home/shimin/test2.js']
You could almost definitely clear the version RequireJS has in cache, forcing it to reload it, although I suspect the node would just serve up the old file again.
Finally, maybe take a look at hot-reloading, which seems to work around the caching without needing to restart the server (example).
Like the OP, I was looking for a way to make development in node with RequireJS easier and faster. In my case, I am using RequireJS with a grunt watch task in node, and wanted to be able to force RequireJS to always get the latest version of a module from the file system. And happily, I found a very simple solution!
I had to slightly modify Henning Kvinnesland’s suggestion by adding the timeout, but now it is working like a charm:
var requirejs = require('requirejs');
// Set up requirejs.config here...
// Disable caching of modules
requirejs.onResourceLoad = function(context, map) {
setTimeout(function() {
requirejs.undef(map.name);
}, 1);
};

how to restrict global variables or functions in node js

how to restrict global variables and functions in node js?
like: require method
i want to limit use of require method.
i don't want any node app to access "fs" in my node framework which i build on top of express, they can only require modules which i want them to.
and also i want to restrict access to process, global scope .
suppose when i load any js library for any app
like:
var x=require('app1.js');
in my framework
then i want to make sure this app1.js cannot access filesystem using require("fs")
app1.js
var x=require("fs");
exports.hello=function(){
console.log(typeof x.readSync);
}
i want this console to print undefined;
and in this sample
var x=require("helper.js");
exports.hello=function(){
console.log(typeof x.hello);
}
i want this console to print function;
thanks in advance
I'd create a new function that will act like require.
requireSafe = function(param){
if(!isAllowedLogic(param)) return null;
else return require(param);
}
And when someone submits code, you append var require; at the top to prevent them to use the regular require. Or you search in their submission and only approve it if it doesn't contain require nor eval.
Why would you want to do that?
It is not possible to change the way require works, as it is a build-in node.js function.
Try this library,
for a bit of detail of how its security works, from the README :
A Node.js subprocess is created by the Jailed library;
the subprocess (down)loads the file containing an untrusted code as a
string (or, in case of DynamicPlugin, simply uses the provided string
with code)
then "use strict"; is appended to the head of that code (in order to
prevent breaking the sandbox using arguments.callee.caller);
finally the code is executed using vm.runInNewContext() method, where
the provided sandbox only exposes some basic methods like
setTimeout(), and the application object for messaging with the
application site.

Resources