skip to Main Content

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


  1. Try running your server on any free port on your server machine

    var https = require('https');
    https.createServer(options, app).listen(ANY_PORT);
    

    and in your android code specify the port to connect to

    val url = URL("https://test.com:ANY_PORT")
    

    That works for me.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. Install Letsencrypt from certbot.eff.org by following the instructions and use this command. Select the No Redirect option at the installation.

    sudo certbot --apache
    

    Copy the privkey.pem and fullchain.pem from your certificate folder using the below command

    sudo cp /etc/letsencrypt/live/test.com/privkey.pem ~/nodejs-rootfolder
    sudo cp /etc/letsencrypt/live/test.com/fullchain.pem ~/nodejs-rootfolder
    

    nodejs-rootfolder is where you have kept the Node.js server project

    Mention the below code in your index.js or server.js file.

    var express = require('express');
    var http = require('http');
    var https = require('https');
    var app = express();
    
    app.use(cors())
    app.use(bodyParser.json({ limit: '500mb' }))
    app.use(bodyParser.urlencoded({ extended: true }))
    var fs = require('fs');
    
    var privateKey = fs.readFileSync('privkey.pem');
    var certificate = fs.readFileSync('fullchain.pem');
    
    var credentials = { key: privateKey, cert: certificate };
    var httpServer = http.createServer(app);
    var httpsServer = https.createServer(credentials, app);
    app.all('*', function (req, res, next) {
       res.set('Access-Control-Allow-Origin', req.header('origin') || req.header('x- forwarded-host') || req.header('referer') $
       res.set('Access-Control-Allow-Credentials', true);
       res.set('Access-Control-Allow-Methods', 'POST,GET,PUT,DELETE');
       res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization,Access-control-request-headers,i$
       if ('OPTIONS' == req.method) return res.send(200);
       next();
    });
    var port = process.env.PORT || 5001;
    httpsServer.listen(port, () => console.log(`Listening on port ${port}`));
    

    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 all HTTP to HTTPS then use the below config.

    sudo nano /etc/apache2/sites-enabled/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
    
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =test.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
        RewriteCond %{HTTPS} off [OR]
        RewriteCond %{HTTP_HOST} ^www. [NC]
        RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
        RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
    </VirtualHost>
    

    Then perform
    sudo service apache2 restart

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search