URL rewriting not working with iframe expressjs - node.js

I am trying to rewrite the URL of an iframe using express.
Similar code works fine on files available in my public folder (which express knows about thanks to server.use(express['static'](__dirname + '/public').
Jade code:
iframe(width="560", height="315", src="/videos/9bZkp7q19f0", frameborder="0", allowfullscreen=true)
Express code:
server.use(function(req, res, next) {
if (/videos/.test(req.url)) {
req.url = req.url.replace("videos", "embed");
req.url = "www.youtube.com" + req.url;
}
next();
});
Adding console.log(req.url) shows the correct url (www.youtube.com/embed/9bZkp7q19f0), yet express logs a 404 error.
Thanks in advance.

You can't just change req.url and expect the client to actually retrieve data from there. What you need is either a proxy or a redirect.
A redirect will force the iframe to redirect to youtube.com:
res.redirect('http://www.youtube.com' + req.url.replace('videos', 'embed'));
A proxy will actually request the page from your server, download the content, and serve back to the client: (you can use request for this)
req.pipe(require('request')('http://www.youtube.com'
+ req.url.replace('videos', 'embed'))).pipe(res);

Related

How to add readable text to URL with NodeJS Express?

I have links that looks like this on my website:
https://website.com/view?id=8d23iva
When users go to the link, I want the URL to change based on information related the id parameter like this:
https://website.com/view?id=8d23iva/amazing-book-title
I have been messing around changing the req and res object data before the user is sent to the page similarly to this.
app.get('/view', (req, res, next) => {
res.baseUrl = "/test";
req.url = req.url + '/test';
req.originalUrl = req.originalUrl + '/test';
next();
});
As an HTTP server, the primary way you can influence the client (i.e the users browser) is with your HTTP response. To change their location you can return a redirection response.
A redirection response is any HTTP response with a 3XX status code and a Location header set to the new destination. In express you can use res.redirect(/*your location here*/) to send a redirection response. For more information about different types of redirects check out this MDN article.
As a side note, you could also consider making the change to the location using javascript on the client once the page is loaded. Though this will depend on other factors. You could investigate the History api for this.

Appending to url path in express

I am building freeboard using freeboard.io. I am running it on a node server using express. I use
router.get('/', function(req, res, next) {
res.sendFile(path.join(__dirname + '/index.html'));
});
to send the freeboard html file when the base route is hit. However, in order for freeboard to load my saved dashboard I need to append #source=dashboard.json to the url. So the final url would look like
http://localhost:8080/#source=dashboard.json
is there some way I can do this using express? Pretty much when I hit localhost:8080/ I want to append to the url path #source=dashboard.json and respond with the index.html file. Thanks!
The fragment section of the URL is never sent to the server by the browser. See here for more info. Therefore for the server the fragment will always be missing even if the user has entered it on the URL field. In this case redirecting the browser back with the same URL inclusing the fragment may be the wrong thing.

404 Not Found error after Parse migration

I've followed the migration guide and setup the Mongodb, NodeJS and Parse server locally on our Linux server. I'm able use the REST API to login to the game and download game-related files that are still hosted by Parse's S3. However it seems like whenever I'm doing a POST or PUT http requests I receive a 404 not found error.
So far I've tried:
1. Enabling the HTTP interface in /etc/mongod.conf
2. Checked the post URLs and they look correct. For logging out I'm sending a post request to http://<server-ip-address:1337>/parse/logout
I'm thinking there might be something wrong with the setup on the server.
Did anyone run into similar problem?
Thanks.
Okay I found the solutions.
Logout problem:
Unity's WWW class seems to work only with valid postData. I didn't provide postData when creating a WWW instance since logout only required custom HTTP request headers. It worked after I created a dummy byte array and pass that to the WWW constructor.
WWW www = new WWW(url, null, headers) // return 404
WWW www = new WWW(url, new byte[1], headers) // worked
PUT problem:
My request headers had 'X-HTTP-Method-Override' set to PUT but it had no effect on the server until I modified allowMethodOverride function inside middlewares.js
var allowMethodOverride = function allowMethodOverride(req, res, next) {
if (req.method === 'POST' && req.body._method) {
req.originalMethod = req.method;
req.method = req.body._method;
delete req.body._method;
}
// Also detect these override request header sent by Unity clients
else if (req.method === 'POST' && req.get('X-HTTP-Method-Override')) {
req.originalMethod = req.method;
req.method = req.get('X-HTTP-Method-Override');
}
next();
};

Node.js : relative and absolute paths

There is something I have trouble with when serving pages (in my case, nodejs).
Let's say I have the following url : http://localhost:80 which serves a simple html page, but you must be logged in, either way you are redirected to a login page (http://localhost:80/login).
That's easy, and everything works fine.
Now let's say I have another url http://otherdoamin.com/internal/back that redirects to my server listening on the port 80.
I am having trouble to find what's the best way to manage my server to work on both url.
Here's a code sample just to show the logic :
function checkAuth(req, res, next) {
//if user isn't logged, redirect to login
}
app.get('/login', function(req, res) {
//if user is logged, redirect to base path (/) which serves an html page
//else
//display login.html page
})
app.get('/', checkAuth, function (req, res) {
res.sendFile(__dirname + '/views/index.html');
});
Let's say now that I have the following case :
I go to http://localhost:80, but I am not logged, I am redirected to the login page (http://localhost:80/login). Then after logging in, I am redirected back to http://localhost:80.
But if I do the following case with my other url (http://otherdoamin.com/internal/back), I'll have problems with the paths : if I have absolute paths, the redirection to login will work, but after logging in, I'll have a redirection to http://otherdoamin.com/ instead of http://otherdoamin.com/internal/back/, which is obvious.
Option 1:
Set otherdomain.com/internal/back as the base url in the Reverse Proxy Path definition.
Option 2:
Store the fully qualified original request url:
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
And then use that to redirect;

transmission rpc with expressjs

i currently have transmission-daemon web ui served by nginx
server {
listen 2324;
server_name torrent.example.com;
location /rpc {
proxy_pass http://127.0.0.1:9091/transmission/rpc;
}
location /transmission {
proxy_pass http://127.0.0.1:9091/transmission;
}
location / {
proxy_pass http://127.0.0.1:9091/transmission/web/;
}
}
i am trying to display this page via https://github.com/stormpath/stormpath-express-sample this dashboard/user interface
in routes/index.js i have
router.get('/torrent', function (req, res, next) {
if (!req.user || req.user.status !== 'ENABLED') {
return res.redirect('/login');
}
var newurl = 'http://127.0.0.1:2324'
request(newurl).pipe(res)
});
i see the html when i goto /torrent but no images/css/js i am thinking the request is not the right tool for this purpose could some one offer a better solution
many thanks
Your HTML probably refers to CSS/images/etc using URLs such as /index.css. The browser makes these into fully-qualified URLs that look like http://torrent.example.com/index.css, which is not proxied by ngnix the way you have it set up.
You probably want to either use URLs such as /transmission/index.css for your CSS (when specified in the HTML), or alternatively have a <base> tag in your HTML for that.
ok so i have made progress with html/css i moved the transmission interface into the express root and imported the html into jade but now i am having a new problem
when i load the /torrent page i can seen in the web console it makes a request to /rpc which i have made a route for
router.get('/rpc|/rpc/', function (req, res) {
var newurl = 'http://127.0.0.1:9091/torrent/rpc/'
request(newurl).pipe(res)
});
but this comes up with a 404 when i change router.get to router.post i get a 405 error
i have removed the 409 error from transmission so this should work
i have solved the issue
i imported the transmission index.html into a jade template
i routed /torrent to render that template
then i made a new route for /rpc|/rpc/ made that do a post request to the backend transmission-daemon
i also changed /js/remote.js to look for the RPC._Root at the atchual domain

Resources