Add a REST API server on a different port
Unanswered
Lilac posted this in #help-forum
LilacOP
I need to add a dedicated REST API server to my next.js application.
It should answer on its own port and with its own authentication method.
It should expose a subset of the data (coming from the application) to a client on the network.
Can I do this directly inside next.js? Or should I add something else (nginx)?
It should answer on its own port and with its own authentication method.
It should expose a subset of the data (coming from the application) to a client on the network.
Can I do this directly inside next.js? Or should I add something else (nginx)?
19 Replies
you will need to create a different app for this, it can't be done in nextjs itself
but if you remove the
own port
it can workLilacOP
thanks. I can use the same port, but I need to restrict the access from the client to the API.
I mean, I don't want that the external client can access the "backend" APIs, but only the subset I decide to be available for the client
@Lilac I need to add a dedicated REST API server to my next.js application.
It should answer on its own port and with its own authentication method.
It should expose a subset of the data (coming from the application) to a client on the network.
Can I do this directly inside next.js? Or should I add something else (nginx)?
White Ibis
Nextjs frontend and backend are on the same port, and if you want split ports, you can use Nginx that map :port to :next-port/api
But don't prefer this, why do you want different port for it?
But don't prefer this, why do you want different port for it?
if you expose anything over a network, you can't restrict people from access it but you can add validations and other things to only allow certain people
you can maybe have an ip whitelist
LilacOP
Hoi @White Ibis I can use the same port. The important thing is that the external client can access only to the specific API I decide. I don't want to expose all the APIs of the application
White Ibis
you can use Nginx that map specific port to original link
for example: :5000 to :3000/api/.../target
it can be used to restrict an user from accessing the paths you don't want
or you can use Bun's api to create a secondary http server
i think node can do it too, but i'm not sure
this if you want another port
LilacOP
@Diamond Master what do you mean with 'middleware'?
@Lilac <@573903151472836609> what do you mean with 'middleware'?
the nextjs middleware
look it up on the wiki
it explains it