I’m working on a code repository where I can combine both Next.js server APIs and Node.js to take advantage of their strengths.

Specifically, I want to route all requests to “/api” to Node.js, while handling other requests with the Next.js application. Has anyone tried this approach before? If so, was it worth it, and how did you make it work?

  • dharmikjagodana@alien.topOPB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    In that situation, we can configure Nginx to route requests accordingly. We can set it up to forward requests with a path starting with “/api” to our Node.js application, and route all other requests to our Next.js application.
    server {
    listen 80;
    # Redirect /api requests to Node.js
    location /api {
    proxy_pass http://127.0.0.1:3001; # Assuming your Node.js app is running on port 3001
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
    location / {
    proxy_pass http://127.0.0.1:3000; # Assuming your Next.js app is running on port 3000
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
    }