I had trouble studying.
I’m making my own sns app like Facebook or Instagram.
So I completed the timeline feature, so this time I’m going to create a chat. So I want to make a chat function using node.js + socket.io.
That’s why I wonder how to build a node.js server.
My development environment is Ubuntu 18.04 + apache2 + Mysql (+ Let ’s Encrypt SSL And I have a domain)
Even if I searched on google and looked up I couldn’t create an https node.js server.
Can’t someone help me easily and in detail?
android is from Api 29
https communication is recommended by default. You can communicate with http on api 29 but that’s not a long term solution. That’s why I configured my server with https. I will never change it to http.
Assume the Domain is called test.com.
The following is the setting in my 000-default.conf file.
vi /etc/apache2/sites-availabls/000-default.conf
<VirtualHost *:80>
ServerName test.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Redirect / https://test.com/
</VirtualHost>
<VirtualHost *:443>
ServerName test.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateKeyFile /etc/letsencrypt/live/test.com/privkey.pem
SSLCertificateFile /etc/letsencrypt/live/test.com/cert.pem
SSLCACertificateFile /etc/letsencrypt/live/test.com/fullchain.pem
<Directory />
AllowOverride All
Options All -Indexes
Require all granted
</Directory>
</VirtualHost>
Port 80 is known as http communication. So I redirected to https uri.
So I basically know that port 443 is the port for https communication.
Anyway, the important thing is that my Apache server is using port 80 and port 443. Here we want to create a node.js socket server that can chat with https communication. It would be really helpful if you helped me easily and in detail.
Root path of the server -> /var/www/html/
php folder path to use in android -> /var/www/html/android
User profile picture folder path to use on android -> /var/www/html/android/image
I want to create a node.js file in the following path.
File folder for chatting on android. -> /var/www/html/android/chat
I searched on Google but it all failed. Otherwise, all the examples I saw ran only one node.js server.
I tried proxypass or proxypassreverse or Something but Failed.
3
Answers
Try running your server on any free port on your server machine
and in your android code specify the port to connect to
That works for me.
One thing you can do is have a reverse proxy with apache. See the docs: https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
What this really means is that say your php stuff is running on port 80 and 443. However, now you have a node service running on port 3000.
Though, you want to mask port 3000 as port 80. So you can say you have domain.com/chat running your port. So you listen for any routes that hit that path and forward them off to the application running that service.
Install Letsencrypt from certbot.eff.org by following the instructions and use this command. Select the
No Redirect
option at the installation.Copy the
privkey.pem
andfullchain.pem
from your certificate folder using the below commandnodejs-rootfolder
is where you have kept theNode.js
server projectMention the below code in your
index.js
orserver.js
file.The
certbot --apache
will do all the installations and setup for SSL configuration. With the above-listed workaround, you can get it done. However, if you want your server to redirect allHTTP
toHTTPS
then use the below config.