Serving pretty much the same site to different domains with express.js [duplicate] - node.js

This question already has an answer here:
Multiple domains, single node (express) app
(1 answer)
Closed 8 years ago.
I'm trying to build an app that generates individual websites.
The idea is for the owner to register his domain, tell it to my platform, point it to my amazon server (still not sure how too).
Then on my express.js server I serve content based on the domain the request is coming from.

You can get the requested hostname from the headers from a field called 'host'. For a node http.ServerRequest, it's available at request.headers.host. For an express app, it can be accessed from req.get('host'). With the hostname, you can route calls from different hostnames to different responses.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23

Related

Get rid of "appspot" in the domain of my app in App Engine [duplicate]

This question already exists:
How do I prevent the "appspot.com" of my App Engine app?
Closed 2 years ago.
I've googled, read a lot for days. And I still can't get a proper answer, so I'm gonna ask.
I have a custom domain, and I added it to deploy my app with App Engine. When I do, App Engine also creates others urls of my same app, for example:
My domain is example.com, when I deploy, I have four sites with my same website:
IMPORTANT: My ProjectID is the same name as my domain that I bought
example.com (and www.example.com)
example.appspot.com
staging.example.appspot.com
us.artifacts.example.appspot.com
So, how could I kill all those unnecessary urls and only stay with www.example.com and example.com?
(I was thinking of using Express.js to handle this, but I first would like to know if there's a better final solution. I've also tried configuring the dispatch.yaml file with it but it doesn't seem to work on this, and also with a .htaccess file to send 301 but App Engine doesn't recognize this since it's not an Apache server).
DUPLICATED, CHECK How do I prevent the "appspot.com" of my App Engine app?
There is no way to delete the appspot.com domains from any application on App Engine.
If you really want to "get rid of them", or making them not accessible at all, you can probably read the Host header from incoming requests on the application, and then check if the request was made to appspot.com or to your custom domain (this is done at your application level, most frameworks should allow you to do it).
Then you can redirect the request, or just return an error response, or nothing at all.
For example, with flask you can do the following:
from flask import Flask, request, redirect
app = Flask(__name__)
#app.route('/')
def read_host():
host = request.host
if "appspot.com" in host:
return redirect('https://[CUSTOM-DOMAIN].com', code=301)
# else, keep running the application normally, it means that the request
# was made to the Custom Domain
if __name__=='__main__':
app.run('127.0.0.1', port=8080, debug=True)
Can I ask why are you interested on doing this? As far as I know there should be no conflict or issues when the appspot domains are coexisting with custom domains, and you are not getting billed for having these appspot domain names either.

how can my flask app create a subdomain automatically for my user? [duplicate]

This question already has answers here:
How to create dynamic subdomains in a Flask application
(2 answers)
Closed 3 years ago.
I have built a flask application that will be used by restaurants, I want to know how I can make the app give each restaurant a subdomain and own database when they register for an account on our landing page.
This question sounds like it is more about the Domain Name System than Flask.
You're saying that your server will start running, and then days or weeks later a new client will register with some new restaurant_name, e.g. "spicytacos". You would like to create a new spicytacos.example.com FQDN in the Domain Name System, with A or AAAA record pointing at your webserver. Then your flask app will be able to recognize the restaurant's name in request.args and respond appropriately.
If you're running the primary nameserver of example.com with BIND9 or similar, then it's just a matter of re-writing the zone config file and sending a HUP signal.
If you prefer to let some other firm handle nameservice details, then probably a * wildcard record is your best bet. It should be a CNAME or address record pointing to your server.
A middle path would be to use a DDNS (dynamic DNS) service, perhaps from afraid.org or No-IP. That is another way of binding a FQDN to an address, which gives you a little more control.
Please refer to How to create dynamic subdomains in a Flask application to see how #app.before_request can parse out novel hostnames during request routing.

CORS locking HTTP request to Elasticsearch server on Angular served on Firebase [duplicate]

This question already has answers here:
How does the 'Access-Control-Allow-Origin' header work?
(19 answers)
Why doesn't adding CORS headers to an OPTIONS route allow browsers to access my API?
(36 answers)
firebase hosting blocking script due to CORS issue
(8 answers)
Closed 4 years ago.
First of all, I'm sure that this is a duplicate question, but I'm a bit far from web techs, and couldn't understand what the hell is CORS, why it is blocking a simple HTTP request and how can I bypass it.
I'm trying to create an Angular 5 + Node.js web app, hosted on Firebase Hosting, and have an Elasticsearch instance on Google Cloud Platform. All I need to do is send 2 very basic HTTP requests to the ES instance from this web app. I'm already sending these requests from mobile apps and Postman app, and there is no problem. But web app logs Preflight response is not successful error. I googled the error and see that it is thrown by CORS. As I said, I have no idea what the hell is CORS and how to bypass it. Any help please (simple help to a non-web-developer). Thank you.
Shortly, CORS (Cross-Origin Resource Sharing) is a security mechanism enforced by default by the browser which specifies what resources (on which servers) your application can use (make requests for). This is a good explanation of CORS - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS.
So you have to set on your server the domain of your application as being one of the domains allowed to make calls. If you do so, the preflight request will return with an Access-Control-Allow-Origin header set and the browser allows the actual request to be sent.
You can have a look here to see how to do it in Firebase - https://groups.google.com/forum/#!msg/firebase-talk/oSPWMS7MSNA/RnvU6aqtFwAJ

Host homepage on a different server than the rest of site [duplicate]

This question already has answers here:
possible to resolve DNS based on URL path
(3 answers)
Closed 5 years ago.
Is it possible to host one page of a website (example.com/index.html) on a different server than the remainder of the site (example.com/blog/ and etc.)? Are there ways to manage this through DNS settings? An .htaccess file?
Thanks!
Short answer is, not really. A DNS entry maps a hostname (example.com) to an IP address. The path part of the url (/index.html) is not part of the hostname, and is only contained inside the http headers.
The general practice for this kind of situation is to use a proxy or load balancer which will send requests to different servers depending on the request path.
If it really needs to go to two different places without a central load balancer, then you probably need a unique subdomain for each site.

send cookies from subdomain [duplicate]

This question already has answers here:
Setting cookies for subdomain
(2 answers)
Closed 7 years ago.
I'm building a web app that uses http only cookie sent by the backend for session data. Now my backend is hosted at xyz.domain.com
My client side is domain.com (note - m not using www.domain.com )
Now when the client receives cookie , the browser doesn't save it. In crome i saw the cookie isn't first party.
After doing a bit of research i figured out to send cookie with domain - .domain.com
Tried that. Didn't work.
How should this be done.
Thank you
You should set domain attribute as your domain:
domain= .domain.com
If you can say which language are you working on and what have you tried so far, people may provide some good examples.

Resources