Any simple way to create link in ExpressJS? - node.js

Sometimes i need create a absolute link in a web site.
Like http://example.com/xxxx/xxxx?foo=bar not /xxxx/xxxx?foo=bar
Here has anyway to create a simple helper for express manually?
Just like:
app.locals.url = function ( pathTo, opts ) {
// Somethings here.
}
I think they may need SSL check, host and route check or somethings.

You can create the full URL :
var URL = req.protocol + '://' + req.get('host') + req.originalUrl;
Them You can redirect to this URL

Related

How can I get the current URL from app request in Node JS/ Express?

I don't know if it is possible but im trying to save the URL that came from app.use in a global variable and trying to access it in the next line. It turns that in the second line the variable is undefined.
Here is my code in my Node JS file:
app.use(function(req, res, next) {
global.CurrentURL = req.protocol + "://" + req.get('host') + req.originalUrl;
});
console.log(global.CurrentURL);
enter image description here
I need to save the current URL in the page so I can make validations with it. I think that the program doesn't go through that piece of code (app.use) is there any way I can force it to go through?

Google cloud functions replaces double slash in url

Im trying to deploy cors-anywhere on Google Cloud Functions. Im supposed to provide the url after gcp's link.
It looks like this :
https://us-central1-my-project.cloudfunctions.net/my-function/http://dummy.restapiexample.com/api/v1/employees
but it's transformed to :
https://us-central1-my-project.cloudfunctions.net/my-function/http:/dummy.restapiexample.com/api/v1/employees
All the double slashes after the host are transformed to simple ones.
I tried replacing req.url to transform http:/ to http:// but still wont work. Maybe this needs to be fixed in the webserver level.
Here's my function in GCP
var cors_proxy = require('cors-anywhere').createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: [
'cookie',
'cookie2',
],
// See README.md for other options
});
exports.myFunction = (req, res) => {
req.url = req.url.replace('/my-function/', '/'); // Strip '/my-function' from the front of the URL, else the proxy won't work.
return cors_proxy.emit('request', req, res);
};
Anyone tried to deploy this in a serverless function?
You're using req.url which contains a normalized version of the request URL. You'll want to use req.originalUrl which, as the name suggests, retains the original requested URL. See Express docs for more info.

Nodejs dynamic OG tags

Using Express. Here's the route. It basically just passes the url params into a template that renders the OG tags.
router.get('/share/:redirectURL/:title/:description/:img', function (req, res) {
var url = req.protocol + '://' + req.get('host') + req.originalUrl; // points to this endpoint
res.render('share', {
url: url,
title: decodeURIComponent(req.params.title),
img: decodeURIComponent(req.params.img),
description: decodeURIComponent(req.params.description),
redirectURL: decodeURIComponent(req.params.redirectURL)
});
});
module.exports = router;
And here's the share template that it renders to.
doctype html
html
head
meta(property="og:url", content="#{url}")
meta(property="og:image", content="#{img}")
meta(property="og:title", content="#{title}")
meta(property="og:description", content="#{description}")
meta(property="og:type", content="article")
body
script.
location.replace("#{redirectURL}");
...and it works! But it only works LOCALLY. As soon as I upload to the server, things go awry.
works: http://localhost/share/http%3A%2F%2Fgoogle.com/Hear%20some%20music./http%3A%2F%2F201.23.456.789%2F%2Fassets%2Fimgs%2Ffavicons%2Ficon1024.png
doesn't work: http://123.45.678.910/share/http%3A%2F%2Fgoogle.com/Hear%20some%20music./http%3A%2F%2F201.23.456.789%2F%2Fassets%2Fimgs%2Ffavicons%2Ficon1024.png
Something upstream is partially decoding the url BEFORE it gets to the Express router. The result is this confused, useless thing.
http://123.45.678.910/share/http:/google.com/Hear%20some%20music./http%3A%2F%2F201.23.456.789%2F%2Fassets%2Fimgs%2Ffavicons%2Ficon1024.png
Switched to query parameters and it works!

node.js http-proxy proxy based on url

I would like to create a proxy based on URL, such that you can go to: blah.com:8000/tolley-ltm and it proxies that request to my local workstation such as tolley.internal.blah.com based on the URL. So I could also do blah.com:8000/someguy-ltm and it goes to some guy's workstation at someguy-ltm.internal.blah.com. The end user will only ever see blah.com:8000/tolley-ltm
I have this sort of working, but not 100% and I would like your help! Here is the current code:
var fulltld ="internal.blah.com";
var proxyServer = httpProxy.createProxy();
var options = {
'/tolley-ltm': 'tolley-ltm',
'/someguy-ltm': 'someguy-ltm'
}
require('http').createServer(function(req, res) {
var dest = req.url;
req.url = '/';
fail = function(err){
console.log("Something bad happened: " + err);
}
proxyServer.web(req, res, {
target: {
host: options[dest] + '.' + fulltld,
port: 6109
}
},fail);
}).listen(8000);
So what happens currently is I go to blah.com:8000/tolley-ltm in my browser and it successfully goes to tolley.internal.blah.com:6109, however when I navigate since I changed the req.url to / instead of tolley-ltm every subsequent actions then goes to blah.com:8000/ instead of blah.com:8000/tolley-ltm and this causes the proxy to stop working.
Sidenote: If I don't change the req.url to '/' it ends up proxying to tolley.internal.blah.com:6109/tolley-ltm instead of just tolley.internal.blah.com:6109/
Is there a way I can keep the url to the end user looking like blah.com/8000/tolley-ltm and have all actions call back to blah.com/8000-ltm, such as if I clicked a link to go to /products, it would take me to blah.com/8000/tolley-ltm/products instead of blah.com/8000/products

get auth portion of url with express.js

I'm building out an API using express.js
(its my first time doing this)
I want to authenticate requests by using the standard auth portion of the url. I want requests to come in as https:// {public-key}:{private-key}#host:port/path
I can't find the auth portion of the url anywhere. req.url is just /path
I found this How to get the full url in Express? which said to do the following:
req.protocol + "://" + req.get('host') + req.url
But that returns only https:// host:port/path
Any help would be great.
As a side note, if this isn't the standard way to authenticate APIs please let me know!
You can use express.basicAuth() middleware to get the username and password sent with the URL.
app.get('/',
express.basicAuth(function(username, password){
console.log(username);
console.log(password);
return (true);
}),
function(req,res){
...
});
Or like the conventional app.use fashion
app.use(express.basicAuth(function(user, pass){
return 'tj' == user & 'wahoo' == pass;
}));

Resources