How to set up local subdomains for Node.js app - node.js

I am running an express app on node.js. The app uses the express-subdomain module to help handle routes for two different subdomains (sub1.example.com and sub2.example.com). I'm hosting the app on AWS Elastic Beanstalk. In my production environment, everything works great. But on my local machine, I cannot get this to work. I tried adding the subdomains to my host file 127.0.0.1 localhost sub1.localhost sub2.localhost. Although that allows me to prepend a subdomain to localhost, the module doesn't recognize this as a valid subdomain, and therefor searches for subdomain routes in my root routes.
In main.js:
var routes = require('./routes/index')(passport);
var sub1_routes = require('./routes/sub1')(passport);
var sub2_routes = require('./routes/sub2')(passport);
app.use(subdomain('sub1', sub1_routes));
app.use(subdomain('sub2', sub1_routes));
app.use('/', routes);
I need to be able to handle this locally. It takes to much time to push a small change to AWS test, iterate, etc.

I'm the author of the module :)
For each new subdomain you wish to test locally you must add into your /etc/hosts file. So for example:
localhost is:
127.0.0.1 localhost
a new subdomain would be..
127.0.0.1 sub1.localhost
and another..
127.0.0.1 sub2.localhost
Check out what I have done in the tests.

I had same exact problem and I found a simple solution. Instead of writing sub1.localhost try replacing localhost with lvh.me this is a domain that always resolves to localhost and now whenever you write sub1.lvh.me even though a port like sub1.lvh.me:3000 it will still work.

There is an awesome website, which someone hosted for all of us.
localtest.me
All requests will be routed to 127.0.0.1, including subdomains.
e.g something.localtest.me:3000
will resolve to 127.0.0.1:3000
but, for example, in your Express app, if you do
app.get('*', (req, res) => {
console.log(req.subdomains); // [ something ]
});
you'll get your subdomain

on Ubuntu
For creating subdomains for localhost you just need to follow 2 simple steps.
Open your terminal by pressing CTRL + ALT + T then run the following commands:
sudo vi hosts
sudo -i gedit /etc/hosts # to edit /etc/hosts file
Once you run 2nd command /etc/hosts file will open and now this is the place where you need to define subdomains.
Example: localhost is:
127.0.0.1 //our localhost
define new subdomain:
127.0.0.1 example.localhost # first
and another..
127.0.0.1 demo.localhost #second
that's it. Hope this was helpful.

Related

How to setup Caddy to get HTTPS on my server

I've been issues to get the HTTPS address for my server. Let's say I have a domain www.mydomain.com
If I run this command it just works fine. I can get the HTTPS.
caddy -host www.domain.com
But I have some proxies that I use for django. So I have a CaddyFile. This is how the CaddyFile is set:
# Django
www.mydomain.com {
root /root/my_projects/my_project
proxy / 127.0.0.1:8000 {
transparent
except /static
}
log /var/log/caddy.log
So if I run this command
caddy -host CaddyFile
, it's not giving me HTTPS. Instead this is what the output is:
Activating privacy features... done.
Serving HTTP on port 2015
http://.:2015/caddyfile
So how should I configure the file or what command should I use to get HTTPS on my server with the proxy and the root folder that I set in the CaddyFile?
Thanks.
I'm guessing you use caddy v1.
From the caddy docs said:
-host
The default hostname or IP address to listen on. Sites defined in the Caddyfile without a hostname will assume this one. This is usually used with -port to quickly get simple sites up and running without a Caddyfile.
The -host option maybe ignored your Caddyfile.
If your Caddyfile is in the same directory with caddy binary, try remove all args, just run caddy. It will automatically picks up the Caddyfile.
Otherwise, try this caddy -conf <path/to/your/Caddyfile>

Namecheap: Node JS Express App - App Route return 404 not found

Trying to get Simple Express Application up using NameCheap Shared Hosting.
I have set up my Node JS application as Described here NodeJS NameCheap Docs
Current Setup:
Application Root: url.com
Application URL: url.com
Application Startup File: server.js
I have ran NPM Install using the button provided
I have tried loading the URL http://url.com/hello Expecting Hello World to displayed in the Page.
var express = require("express");
var app = express();
const port = 3001;
app.set("port", port);
app.get("/hello", function(req, res) {
res.send("hello world");
});
app.listen(app.get("port"), () =>
console.log("Started listening on %s", app.get("port"))
);
The results I am getting when navigating to http://url.com/hello:
Not Found
The requested URL /index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Namecheap only tells you how to configure the nodejs app however their hosting is based on cPanel which requires you configure the webserver (apache generally). Once you get an application running there’s a special button to register it for the apache configuration aka let it run from your domain. I don’t know the steps by heart but you should ask NC support to direct you to their documentation for configuring apache to run a nodejs app you configured.
If they do not link an article from their knowledge base use this link: https://confluence1.cpanel.net/plugins/servlet/mobile?contentId=17190639#content/view/17190639
Basically what you need now is to configure cPanel or ssh into your server and test your app locally. There’s a number of things that could cause your issues like incorrect apache configuration (your default port 80 is looking for php app), port not open/firewalled, application not registered - and all of this is cPanel specific.
To make sure you are reading the correct document check in namecheap cpanel for the docs button and review all the above. It should be obvious what needs configured - your nodejs code is probably not the cause here
In my case, it was the problem with .htaccess file. Adding the following rules in my .htaccess file present in the website's public directory helped me:
# CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/<user>/<your_nodejs_app_folder>"
PassengerBaseURI "/."
PassengerNodejs "/home/<user>/nodevenv/<nodejs_app>/<version>/bin/node"
PassengerAppType node
PassengerStartupFile <startup_script>.js
# CLOUDLINUX PASSENGER CONFIGURATION END
Make the required changes in the above rules before pasting them in your .htaccess file. Also, just in case, make sure the port you are using is open, via customer support.

How to run express app on port 80 without sudo access

I am using the express framework for nodejs on a Dreamhost VPS and I want to run my server on port 80 but all of the answers I've seen require sudo/root access but on a Dreamhost VPS I am not given this permission. I can't edit any of the Apache files. The only thing I am able to do is create a .htaccess file. How can I achieve this?
You can try to run Node on a different port (greater than 1024, since those of 1023 or lower require root) and just proxy the requests through Apache by adding something like this to your .htaccess file:
RewriteEngine On
RewriteRule ^/(.*)$ http://127.0.0.1:8080/$1 [P,L]
(In this example Node would be running on port 8080).
For anyone who comes across this and has Dreamhost, the solution I came up with was creating a proxy. In the Dreamhost panel there is an option to create a proxy for a domain that you can then direct to go to any port for that domain and I simply made the proxy go to port 3000 and then my node server ran on port 3000.
#Frxstrem's answer also works but it failed to work when I shared a link and is kind of a hacky solution.

Creating a local custom host name instead of localhost?

Currently, my flask app runs locally on:
http://localhost:5000/some_page
How could I create a local custom location for my app like:
http://myappname/some_page
Sort of like a local domain name. Is this possible at all? Any pointers would be great.
In order for the browser to resolve this custom name, you will need to add an alias to your /etc/hosts file. It probably already contains a line about 127.0.0.1, in which case you just add your alias to the list
127.0.0.1 localhost localhost.localdomain myappname
You can then change the server name in the app's config to make it explicitly use this name.
app.config['SERVER_NAME'] = 'myappname:5000'
Only privileged programs (run as root or with sudo) can bind to low ports such as 80, so you will still have to use a high port number.
It can be done using SERVER_NAME option in config:
app = Flask(__name__)
app.config['SERVER_NAME'] = 'myappname:80'
More information here:
http://flask.pocoo.org/docs/0.10/config/

Assigning a domain name to localhost for development environment

I am building a website and would not like to reconfigure the website from pointing to http://127.0.0.1 to http://www.example.com. Furthermore, the certificate that I am using is of course made with the proper domain name of www.example.com but my test environment makes calls to 127.0.0.1 which makes the security not work properly.
What I currently want to do is configure my development environment to assign the domain name www.example.com to 127.0.0.1 so that all http://www.example.com/xyz is routed to http://127.0.0.1:8000/xyz and https://www.example.com/xyz is routed to https://127.0.0.1:8080/xyz.
I am not using Apache. I am currently using node.js as my web server and my development environment is in Mac OS X Lion.
If you edit your etc/hosts file you can assign an arbitrary host name to be set to 127.0.0.1.
Open up /etc/hosts in your favorite text editor and add this line:
127.0.0.1 www.example.com
Unsure of how to avoid specifying the port in the HTTP requests you make to example.com, but if you must avoid specifying that at the request level, you could run nodejs as root to make it listen on port 80.
Edit: After editing /etc/hosts, you may already have the DNS request for that domain cached. You can clear the cached entry by running this on the command line.
dscacheutil -flushcache

Resources