How to use node.js and sockets within my backbone application - node.js

This should be a fairly simple question to answer I think.
my node.js server is installed at
/usr/local/bin/node
I have an index.html and server.js files located at
/usr/local/bin
When I run node it works fine. I have installed a chat application which runs at localhost:8888 the main application/website runs at localhost:8000. My backbone files and main site is located on my apache server, for arguments sake say /usr/local/apache/html
How can I move my chat application into the main site so that I can access the chat app through node?
I've currently got two parts of the site working on different ports and I need to integrate the chat part.
Any advice on this would be great.
Thanks in advance :)

You need to setup Apache as a reverse proxy using mod_proxy. This will allow you to redirect requests from one port to another, making your Backbone client application only see one server.
So for instance, if you want your chat client available at www.mysite.com/chat, you would need to first install mod_proxy and then setup your site's conf file as such:
ServerName www.mysite.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location /chat>
ProxyPassReverse http://localhost:8888/
</Location>
You can read more about mod_proxy here: http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

Related

Apache configuration 2.4

I need to perform the following configuration on my Apache server, version 2.4, with CentOS system.
The instructions I received are as follows:
Configuring Another Web Server to Serve the Files If another web
server is using port 80 on the same server as Centova Cast, you will
need to use this method.
This method depends upon your knowledge of the other web server you
are using; you must be familiar enough with your web server of choice
to configure it appropriately. Centova Technologies cannot provide
support or assistance with any changes to your web server's
configuration.
To allow "Let's Encrypt" to validate your domain ownership, you must
configure your web server such that any requests for files under the
following URI (replacing example.com with your own domain name):
http://example.com/.well-known/acme-challenge/ ...are configured to
serve files from the following directory:
/usr/local/centovacast/etc/ssl/acme-challenges/ So for example, if you
create a file called
/usr/local/centovacast/etc/ssl/acme-challenges/test.txt, you should be
able to visit http://example.com/.well-known/acme-challenge/test.txt
in your browser and see the contents of the test.txt file.
A few example configurations are provided below. Please note, however,
that differences in your web server configuration may require
additional settings not mentioned here.
Example apache:
Alias /.well-known/acme-challenge /usr/local/centovacast/etc/ssl/acme-challenges
<Directory /usr/local/centovacast/etc/ssl/acme-challenges>
Options None
AllowOverride None
Apache 2.x
<IfModule !mod_authz_core.c>
Order allow,deny
Allow from all
</IfModule>
Apache 2.4
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
</Directory>
I've tried everything and I can't get it to work, how should the correct configuration be done?
My configuration file is located at: /etc/httpd/conf/httpd.conf

Proxy a node/react application in an apache server

Using forever to forever run the node server on the virtual machine, I am unable to get the app to run without explicitly adding the port in the url like so: URL.com:8080
If I don't use the port in the URL, I do load up the file structure of the application.
Steps to reproduce: I have a create-react-app application.On the virtual server I run 'npm run build' to make sure I have a build to serve. I then run forever start on the root of the application.
The code below should give all the necessary details. I can provide more if you need.
I have spent so much time tweaking the .conf file to try different configurations but I can't seem to get it. I am using it and successfully hosting two static html sites but not this node application.
Package.json:
...
"main":"server/index.js",
"proxy":"http://localhost:8080"
...
Apache url.conf:
<VirtualHost *:80>
ServerName URL.com
ServerAlias URL.com:8080/
DocumentRoot /var/www/nameOfApp/
<Directory />
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/nameOfApp/public>
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
</Directory>
</VirtualHost>
Node server file using express:
app.use(express.static(`${__dirname}/../build`));
I've also made sure I have the modules enabled to allow for proxying. So I think essentially, what I need is to request this site and not need the :8080 at the end.
Apache and NodeJS are 2 different and separate application.
You interact with them by sending request to the port that they are listening to. In your case here,
Apache is listening at port 80
Your NodeJS application is listening at port 8080
So all request to port 80 will be handled by Apache, and since you do not has an index.html, Apache will default to just list out the files and directory (Options Indexes). Up until this point, your node application do not know anything about your request.
So what you need to do is define some endpoint, say url.com/node, and tell Apache to forward all request of this endpoint to port 8080 and let your node application to do the job.
How to do this?
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass
Best practices when running Node.js with port 80 (Ubuntu / Linode)
Node.js + Nginx - What now?
Hope this points you to the right direction.
The configuration that eventually worked was as simple as this:
<VirtualHost *:80>
ServerName yourdomain.com
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
</VirtualHost>
Looks like I was over complicating it trying to create a complicated proxy but the solution was very simply adding this to the config for the node application and then running sudo systemctl restart apache2 and everything worked beautifully.

How to properly launch Angular Universal to live server

I have my website setup and working by modifying the universal-starter seed found here: https://github.com/angular/universal-starter and I have got my site working (rendering HTML) on the localhost node server, now I have a SSH connection and node installed, now I am here:
And I am not sure where I put my /dist files etc and then start the nodejs server.
NodeJS/npm installed shown here:
Edit (3/23/21): Something to be aware of when using Universal
You really need to consider writing blocks of code specifically for the Browser or for the Server. For example:
You might use an external SAAS solution (like SharpSpring or Google Tag Manager) to help your team track leads and generate automatic workflows.
Some of these services require you to write objects to either the window or document objects. Because your server isn't running javascript, there isn't a way to get these objects to behave like they would natively when running your application on the server. You will need to really mess around with these things.
A really helpful article I found was this demo of real app problems using Angular Universal.
You will also need to dynamically append imported scripts to your index.html header. I have seen this same concept a couple of different places, but here is one as well. There might be a stack overflow answer that has this same idea.
However, in some instances, you will need to use a library or something (like Postscribe) to dynamically add scripts to individual components inside of one of your html template files. Postscribe helped us drastically by allowing us to dynamically import a specific script in front of a form in one of our components. I highly recommend using Postscribe.
So, long story short, you will need to make sure you are importing scripts and appending them to your index.html file only if your application is running in the browser. Do this check for all external JS files as well that you are importing into your application and using directly. Start by injecting PLATFORM_ID into whatever component you need to make this check in, and using it that way
This applies to all kinds of things like animations (which might be listening to a window scroll event), etc.
Original post below:
#Cacoon I hope this is what you did.
This first part is an existing answer from m98 on this Github issue. Just posted here for clarity
Build your app: npm run build:ssr
Move the dist over to your server
install PM2 npm install pm2 -g
On your server, use PM2 to run the server bundled app pm2 start dist/server.js
If you're using Nginx, or other web servers, make sure to redirect requests to the port that the app started with PM2 is listening on.
UPDATE (2/7/2020)
For Apache make sure your document root is specified as your dist/browser folder like
DocumentRoot "${SRVROOT}/htdocs/dist/browser"
<Directory "${SRVROOT}/htdocs/dist/browser">
And try this in the bottom of your httpd.conf file:
<Location />
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html
</Location>
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://localhost:4000/
ProxyPassReverse / http://localhost:4000/
</VirtualHost>
<VirtualHost *:443>
ServerName localhost
ServerAlias localhost
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / https://localhost:4000/
ProxyPassReverse / https://localhost:4000/
</VirtualHost>
You have to set the virtual host in the httpd.conf, and not in a .htaccess file. You can set Mod_rewrite at the same level of your index file in an .htaccess file, but you cannot set virtual host at that level.
Also, make sure you enable the modules for the proxy, like mod_proxy_express in your httpd.conf file
This is for developing locally. Change localhost to your domain when deploying to prod.
This was actually easier than it seemed, I just needed to install pm2 with npm so I could make the nodejs process constant, then use RewriteEngine settings in the .htaccess file where I wanted to my angular universal application to render (it was in a subdomain folder) and ensure the port is correct and it linked perfectly and loads html in source allowing proper SEO and indexing

Node.js and Apache for single domain same server

I built a RESTful website, and it is written in Angular2(front end) and Php(backend). Since I am using Angular Universal, I need Node.js to pre-process the Angular scripts. How can I configure my server to host both Node.js and Apache? I searched online, but can't find any resources related to "sharing Node.js and Apache on SINGLE domain"...
How to configure my Apache to route "everthing" to the /dist/ folder (and served by Node.js) except for the URI that are prefix with /api/ and /uploads/ (route to the /php/ folder).
[Back-end] Api Php script:
If www.example.com/api/?...?, then use this script /var/www/example.com/public_html/php/api.php to process it.
Uploaded images:
If asking for uploaded img files (www.example.com/uploads/[img*.png]), then try to look inside this /var/www/example.com/public_html/php/uploads/[img*.png] folder.
[Front-end] Html, Js, etc...:
Everyting www.example.com (except the /api/ and /uploads/), then ask Node.js to run it from this /var/www/example.com/public_html/dist/ folder.
I guess it is related to proxy and proxy_http, but I don't know how to configure it since I can't find any resources related to sharing both systems for single domain...
i think you should Run Node.js on a different port (e.g 8080) and use Reverse proxy in Apache config (virtual hosts) like this:
ProxyPass /api http://www.example.com/php/
ProxyPassReverse /api http://www.example.com/php/
ProxyPass /uploads http://www.example.com/php/uploads/
ProxyPassReverse /uploads http://www.example.com/php/uploads/
ProxyPass / http://www.example.com:8080/dist/
ProxyPassReverse / http://www.example.com:8080/dist/
and then use htaccess rewrite rules for your uploads and api
you can read more about mod_proxy

apache configuration for hybris

I have one webserver and two hybris servers. For example:
webserver - http://10.0.0.1
Hybris - http://10.0.0.2:9001 and http://10.0.0.3:9001
Now I want to use webserver as a load balancer to redirect the requests. Redirecing is working fine but during redirect it exposes the hybris IP which I dont want. I want to expose the webserver IP to public.
How I can write the redirect rule for that. Any help is highly appreciated.
Thanks,
Basically, you want to have the following in your apache configuration:
<Proxy balancer://cluster>
BalancerMember http://10.0.0.2:9001 route=node1
BalancerMember http://10.0.0.3:9001 route=node2
</Proxy>
ProxyPass / balancer://cluster/
ProxyPassReverse / balancer://cluster/
By using a proxy like this it will be transparent to the user which ip is being served. See more information on how to configure hybris to worh with Apache in the official documentation here.

Resources