Using node.js libraries on front-end - node.js

How can I use the 'request' node.js module on the front-end?
normally I would retrieve it like so:
var request = require('request');
but this is not possible on the front-end since require is not recognized.
What is the best way to solve this?

To use node modules in the browser you can use a library called Browserify . This allows you to work with the common module pattern as well as the you can use this package browser-request to get the features of request module

Related

Include nodejs js module on client side

E.g. Using SimpleMDE. I've installed it to node modules, what's the right way to include it? Making the nodemodules/simplemde public seems like the wrong move. I'm using express and ejs.

Possible to use http-parser-js in an Electron app?

I need to make an HTTP request to a service that returns malformed headers that the native Node.js parser can't handle. In a test script, I've found that I can use the http-parser-js library to make the same request and it handles the bad headers gracefully.
Now I need to make that work within the Electron app that needs to actually make the call and retrieve the data and it's failing with the same HPE_INVALID_HEADER_TOKEN. I assume, for that reason, that the native HTTP parser is not getting overridden.
In my electron app, I have the same code I used in my test script:
process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
var http = require('http');
var req = http.request( ... )
Is there an alternate process binding syntax I can use within Electron?
This was not an electron issue. My app makes several different requests and most of the are to services that return proper headers. Originally, I was using the request-promise library to handle all calls, but I needed to modify the one call that returned bad headers.
The problem was that I was still using request-promise for the other calls and that library conflicts with the custom code I had to write to deal with the malformed headers. Once I modified my custom code to handle all requests, things worked much more smoothly.

React isomorphic inconvenience

I'm building isomorphic app using Node.js and ReactJS.
I've met a situation when inside some isomorphic React component (rendered both on client and server sides) I need to include only client-side dependency (some notifications library for example).
What is the best way to have it worked (compiled) both on server/client sides?
P.S: I have few thoughts about possible workarounds something like checking for browser env variables (ex. some typeof window !== 'undefined') etc. but I'm not sure it's a good way to go.
Use the lifecycle method componentDidMount which is not invoked on the server instead of checking if window is undefined.
The "downside" is if your client side dependency enhances an element for example or changes any property of it, it'll get the default property first since it was rendered server side and when componentDidMount runs it'll get changed causing a "blink".
If you are using browserify I often use process.browser which is only true in browserified code.
If you wanted to get fancy and remove server code from the browser instead there is also isbrowser which will do just that.
Another way (webpack or browserify) is to take advantage of the browser field in the package.json. You can make it so that the server requires a noop file and the browser requires a file the exposes the client side api.
I have defined variable in webpack configuration file called process.env.WEBPACK and in my code when i need something like bottstrap js or something else i just write
if(process.env.WEBPACK){
//awesome lib included or scss or whatever
}

Using url.resolve in angularjs

Newbie question: I'd like to use Node's URL module (http://nodejs.org/api/url.html) in AngularJS.
e.g.
var url = require('url')
url.resolve('http://example.com/', '/one')
How do I do that?
You have a couple options.
You could use something like Browserify, or you could simply use a different library meant for URL manipulation in the browser.
URI.js comes to mind.

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