Search code examples
node.jsdjangoserveroauthsmartsheet-api

deploying Node.js and Django to same domain/server


Objective: Give my Django App (with Python backend [written] and react/redux/js frontend [written]) a Smartsheet API OAuth page that redirects users off the main website until auth is done, and then back to website (that utilizes Smartsheet API in features)

Crux: A hunch said that the OAuth should be in node.js to match front end more, and I found a working sample code for how to do Smartsheet OAuth in Node. It worked great on its own! Then when I tried to integrate this node.js page into Django, I got errors from which ever server I set up second, that there is already a server on that given (12.0.0.1:{port}) local url. Maybe OAuth should be written in python instead, but I couldn't find sample code for it, so it would be great if I could keep it in Node.

Question- Is there a way to deploy components from both Node and Django to the same server/domain? It's weird to have users go to one site, finish their oauth, just to be pushed to a totally separate domain. This may also pose security risks.

My attempt- I thought I would just create a simple landing page and then after being logged in, shoot the user forward on the website (on a redirect url). This is what the Django urls.py would look like:

from django.urls import path
from . import views


urlpatterns = [
    path('', views.oauth ), //Views.oauth is fairy blank, and I wanted my Node.JS server
                            //to listen at that hostname:port
    path('loggedin/', views.index ), //when oauth ended, I wanted it to send the user here
]

This attempt made these errors: Django Error Node Error

Thanks for any ideas to my inquiry!


Solution

  • Microservices are basically small components which are interconnected by REST or any kind of api and what you are talking about IS microservice architecture.

    Now, you can deploy the Django on some port lets say 8090 and NodeJS in another port lets say 8080.

    Now, to connect them you need to have some kind of reverse proxy easiest would be nginx.

    So, the rules will be like this.

    If the url is host/api forward traffic to NodeJS 127.0.0.1:8080 port. Otherwise forward traffic to 127.0.0.1:8090.

    A example would be this question: NGINX - Reverse proxy multiple API on different ports

    server {
    {
        listen 443;
        server_name localhost;
    
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    
        location /api/orders {
            proxy_pass https://localhost:5000;
        }
        location /api/customers {
            proxy_pass https://localhost:4000;
        }
    }
    

    So the rule says that if the location is /api/orders go to localhost:5000 and if it's /api/customers go to other one, the 4000 port one.

    Now, you can research about reverse proxy and probably come up with your own rule.