this post was submitted on 01 Nov 2023
1 points (100.0% liked)

Side Project

29 readers
1 users here now

A community for sharing and receiving constructive feedback on side projects.

founded 1 year ago
MODERATORS
 

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?

you are viewing a single comment's thread
view the rest of the comments
[โ€“] palayoub@alien.top 1 points 1 year ago (1 children)

My stack is:
Separate backend Nodejs project served with REST at api.domain.com
Separate Frontend Nextjs project built with Static Site Generation

And Nginx as reverse proxy

[โ€“] dharmikjagodana@alien.top 1 points 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;
}
}