Node.js forward proxy to serve a web page - node.js

I am trying to set up a forward proxy to serve web pages in nodejs (using the request package).
I can get the web page to be served up, however my problem is in the assets that the webpage tries to reference, they are (of course), all relative pathing (e.g. images/google.png).
my code is thus:
...
app.get('/subdomain/proxy/:webpage', function(req, resp) {
req.pipe(request('http://' + req.params.webpage)).pipe(resp);
}
...
and the response I get, given proxy.mywebsite.com/www.google.com looks like (google inline-styles its css):
So, the question is:
How do I load in resources that are relatively pathed? Is my approach here regarding a forward proxy even correct?
My only solution is to scrape all relative paths and rewrite the html to be absolute references instead which sounds horrific (and doesn't account for cases where the external .js scripts could also relatively reference stuff).
It must be possible as there are websites like 'hidemyass' which achieve the same thing.
This is all extremely new to me, but it seems like I'm asking for something quite simple and I'm quite surprised I've not been able to find a solution yet.

Related

Reverse proxy passthrough for multiple websites

I'm building a website where there will be user generated content, for example under the url website.com. Each user might have a page (of their own content) somewhere like website.com/user/jack, website.com/user/lucy, etc. This number of users (as well as their names) are dynamic and can be added any time. I already have website.com working and deployed.
I'd like to be able for users who have their own domain name, e.g. jacks-website.com, to be able to use that domain name but effectively have it act and behave as website.com/user/jack. By this, I mean the following:
jacks-website.com is has the same DNS records as website.com.
jacks-website.com serves exactly the same content as website.com/user/jack. This also goes for any/all (nested) subpaths, i.e. jacks-website.com/custom-path -> website.com/user/jack/custom-path.
A user browsing jacks-website.com will have NOT be directed to website.com/user/jack. To them it should feel like browsing jacks-website.com directly.
I've tried playing around with Node's http-proxy package, where I would check what req.headers.host is then map it to a corresponding /user/jack path. However, I still couldn't get it to work as it would cause jacks-website.com to redirect to jacks-website.com/user/jack.
Also note that I'm fully using SSG/SSR on my website.com app, so no need to worry about client-site routing or anything.
Thanks a lot.

what is the ''/home" after the website domain name?

I'm new to web development and i want to ask that why some website have the "/"?
for example https://www.roblox.com/home, notice the "/home" what does that called
I have tried to search on google and i can't find the answer
And some website have like "/login.php", "/index.html" it can also be html?
These are URLs (https://en.wikipedia.org/wiki/URL) and they identify the resource you are trying to reach. I would suggest reading more about how web pages works to get a better general overview of things(e.g.: https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/How_the_Web_works)
How these resources are actually interpreted depends on the server side implementation:
.php are usually processed by PHP web server
Other static files such as images (*.png , *.jpg, etc), html files, svgs, CSS, js, etc - Are usually located in the local server by the web server (httpd, tomcat, IIS, nodejs, and many many others) and the files as transmitted to the client 'as-is'
When using online tools to build websites, these complexities are usually abstracted away, and in the end URLs will just mean a resource identifier.
[domain]/[section]/[page(.html|.php)|resource(.js|.css)]
domain: the address of the website
section: a way to navigate inside the website itself
page: the user interface that might be rendered server side of client side hold the controls shown to user
resource: files that changes how the content in the pages looks and behaves like

Custom 404 for media files in Orchard

Is there a a way to create a custom 404 page for files in the Media directory of Orchard? I would like to show a specific error messages when somebody tries to download a file from a specific path (/Media/Default/MySpecificFolder/) that no longer exists. Right now if a file does not exist it just renders a blank page.
Orchard uses StaticFileHandler for images(anything in the media folder), css and js files among others.
there was a chap who tried to create 'SEO' routes for assets. have a look in the modules gallery.
All said, it seems a bad idea to put assets behind too much logic in the days where more people are using CDN or buckets to provide images to their websites and alleviate the load on the server.
You could go oldskool and override the handler. i used to do it all the time, back in webforms. here is some code that seems like the real thing:StaticFileHandler
Perhaps leave the handler alone for the main web.config but override the ones in the folders you might like to serve your special 404 page.
I do not know why would you need something like that, but go for it.
thinking about it, you could create a shape that would abstract the functionality you are after. Im thinking on the sides of documents mostly, cos i still struggle with the images...
Your shape will tokenise the document in route which will pick up an id or url and if the file does not exists redirect to a 404, but if it exists just send the file.
Does it make sense?

Host multiple site with node.js

I'm currently learning node.js and loving it. I noticing, however, that it seems that's it's really only fit for one site. So it's great for hosting mydomain.com, but what if I want to build an actual full web server with it. In other words, I would like to host mydomain.com, example.com, yourdomain.com and so on. What solutions (modules) are available for this? I was thinking of simply parsing the url from the request object and simply reading from the appropriate directory. For example if I get a request for example.com then read from the example_com directory or if I get a request from mydomain.com read from the mydomain_com directory. The issue here is I don't know how this will affect performance and scalability.
I've looked into Multi-node but I don't fully follow the idea of processes yet (I'm a node beginner).
Any suggestions are welcome.
You can do this a few different ways. One way is to write it directly into your web application by checking what domain the request was made to and then route within your application but unless your application is very basic this can make it fairly bloated and can get messy. A good time to do something like this might be if you're writing a blogging platform where everything is pretty much the same across all your domains. The key difference might be how you query your data to display the right data.
In this case you'd probably use the request to see which blog is being accessed.
If you want to just host a few different domains on the same server all using port 80 (like most websites do) you will want to proxy each request off to a different process. You can do this with nginx or even with node itself. It all comes down to what best fits your needs. bouncy is a quick way to get setup doing this as its a nodejs module and has some pretty impressive benchmarks. nginx (proxy with nginx) is probably the most wildly used method though, as a lot of nodejs servers use nginx to serve static content anyways.
http://blog.noort.be/2011/03/07/node-js-on-nginx.html
https://github.com/substack/bouncy/
You can use connect's vhost middleware (which is also available in express) to dispatch requests to separate request handlers based on the Host: header. This assumes that everything is being handled by the same node process on the same port; if you really need separate processes, then the suggestion about using nginx as a reverse proxy is probably the way to go.

Can IIS 6 serve requests for pages with no extensions?

Is there any way in IIS to map requests to a particular URL with no extension to a given application.
For example, in trying to port something from a Java servlet, you might have a URL like this...
http://[server]/MyApp/HomePage?some=parameter
Ideally I'd like to be able to map everything under MyApp to a particular application, but failing that, any suggestions about how to achieve the same effect would be really helpful.
You can set the IIS6 to handle all requests, but the key to handle files without extensions is to tell the IIS not to look for the file.
http://weblogs.asp.net/scottgu/archive/2007/03/04/tip-trick-integrating-asp-net-security-with-classic-asp-and-non-asp-net-urls.aspx
You can also create an ISAPI filter that re-writes urls. The user enters a url with no extension, but the filter will interpret the request so that it does. Note that in IIS it's real easy to screw this up, so you might want to find a pre-written one. I haven't used any myself so I can't recommend a specific product that's any different than what you'd find via google, especially as I don't know your specific use case. But at least now you know what to search for.
You can also rewrite your urls using ASP.Net:
http://msdn.microsoft.com/en-us/library/ms972974.aspx

Resources