Using url.resolve in angularjs - node.js

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.

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

All requests via Proxy in Node-Webkit

Is there a way to force Node-webkit to use proxied settings for all requests ? I know there is an API solution using setProxyConfig() but this doesn't work with authenticated proxy, the prompt shows up to login but crash when submitted...
So I tried to use node request module, and it works fine :
var request=require('request');
var proxy = request.defaults({'proxy':'http://login:pwd#proxy:port'});
But now my problem is to tell node-webkit to use this for each request.
Any solutions ?
I'm quite new using node and node-webkit so maybe there is a better way to do that.
Thanks a lot !
You may try
process.env.http_proxy = 'http://login:pwd#proxy:port'
It will work with request lib, also should impact other requests from node-webkit (assets, ajax)
But may be other libraries don't use environment proxy settings. If that doesn't work you can try https://www.npmjs.com/package/global-tunnel
Not sure if this is solved, but i came up with a simple solution:
i wrote a simple wrapper for the request library
https://gist.github.com/jodevsa/7dd9662b8244359fa0d7626ae7c9bd69
all you have to do is ,
Head to $PROJECT_DIR/node_modules/request/
Rename index.js to core.js
Create a new file called index.js
Copy code content from the above gist link to index.js
Change proxyLoc value to you'r preferred proxy
If you decided to disable proxy , just change value of ON variable to false inside index.js
Cheers :D

Using node.js libraries on front-end

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

Express & Socket.io Route parroting/copying/sharing

I'm working with expressjs and socket.io in nodejs. I'm looking into assign identical route handlers to requests made in either HTTP or via websockets/socket.io.
For instance:
var responder = function(req, res){
req.params //<-- {id: 'something...'}
}
app.get('/foo/:id', responder);
io.on('/foo/:id', responder);
socket.io doesn't appear to have this type of routing functionality. Does anyone know of a library/module to help with this?
There are several options.
If you'd like to keep using express, check out express.io.
If you don't mind using something a bit different, sails lets you do this sort of thing as well.
(Update: sails now uses express too)
Both have been used in production successfully.
Note that routing is also pretty simple to implement on your own. If you check out how express do it I'm sure you'll be able to figure out a slim implementation that would match you needs.
Good luck! Let me know what you ended up using and how it worked for you.

Express Url Generation

I've recently been playing with nodejs and would like to build my first project with it. But there is a major stumbling block for me.
URL Generation.
I'm very used to Codeigniter's base_url() and site_url(), this gave a full url like http://www.example.com/resources/img/bla.jpg, so it's been a bit odd for me to find that there is no such equivalent functions for NodeJS / Express.
Am I going about this wrong or is there a module somewhere that would make it possible to generate urls much like base_url() and site_url() did?
I'm using the Express Framework with Jade as the template engine and MongoDB as the Database.
The scope of Express and the scope of a PHP framework like Codeigniter are quite different, and Express makes much less assumptions on how your site is laid oud. For example, it would be perfectly possible to serve several virtual hosts with Express (using the connect-vhost middleware). In this case there would be little point in having a function like base_url().
This being said, it would be quite easy to roll your own, something like that:
var BASE_URL = "http://mysite.com"; // Can be loaded in a config file
module.exports.baseUrl = function(path) {
path = (path || "").replace(/^\//, '');
return BASE_URL + "/" + path;
}

Resources