Setting and accessing PORT in cli in dev env in [email protected] #12504
-
How can I access the server port number? The default next js port is 3000. This port port does not exist in process.env object. It also does not exist in process.dev when I attempt to set it either by running "next -p 3000" or "cross-env PORT=3000 && next dev" Background: I am getting "127.0.0.1:80 ECONNREFUSED" error when running api calls in the server. I do not know where PORT 80 is coming from: the server port is 3000. My workaround idea is to use the absolute URL. But the process.env.PORT is undefined. As per @leerob request, following is a relevant snippet of the app architecture provided. Api fetch modules are placed in a single utils folder. All remote api requests like those from Redux actions and getInitialProps are made to the fetch modules. Fetch module constructs The idea is to add |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Could you share an example on CodeSandbox or explain your app architecture more, maybe? |
Beta Was this translation helpful? Give feedback.
-
The best I can come up with at this time is: if Hope there is a way to intercept all requests to |
Beta Was this translation helpful? Give feedback.
The best I can come up with at this time is: if
server.getBaseUrl()
does not exist, create it using the port info pulled from current server request headers either from thepages/_document.js
static Document.getInitialProps'
context.req
arg or from thepages/api
routes'req
arg.Hope there is a way to intercept all requests to
pages/api
routes in one place with a middleware call to streamline the process. Or perhaps, I am currently unaware of it. At the moment, I am using next-connect in allpages/api
routes to attach the middleware that pulls the port info.