Next.js Discord

Discord Forum

Can next.js share variables with its custom server?

Answered
Ring-necked Duck posted this in #help-forum
Open in Discord
Ring-necked DuckOP
Say I have a next.js app running with on top of a express.js custom server, can I access the same variable reference wise from getServerSideProps and the express server?
Answered by Ring-necked Duck
I just put the entire context in request
View full answer

48 Replies

Northeast Congo Lion
do you mean you have nextjs frontend on vercel and express on a separate server?
Ring-necked DuckOP
No
I do nextapp.prepare() then attach that to express on / using server.use
server.use("*", nextapp.getRequestHandler())
Maybe this is what i need
Northeast Congo Lion
not 100% sure, not something i've done before
usually servers use APIs to chat to eachother
Ring-necked DuckOP
so I can?
testing with this code:
nextjs should not have to set that variable
🤔 maybe if i use global?
@Ring-necked Duck testing with this code:
have you called dotenv/config on the server
Ring-necked DuckOP
I'm not talking about environment variables
though
I'm talking about runtime variables
oh mybad, i thought you were talking about env
Northeast Congo Lion
i wasn't aware variables can be passed between applications like that
is it even possible?
Ring-necked DuckOP
that's what i'm trying to figure out
so it looks like next and node have different global contexts entirely even if next is started from within express
Northeast Congo Lion
if in doubt
ask chatgpt
its usually not too bad at giving somewhat reliable advice.
Ring-necked DuckOP
worst case scenario, I can just fetch from getServerSideProps
the api fetching to and from is the same process, same PID
a bit inefficient
or ditch ssr
OK i know what to do lmao
Ring-necked DuckOP
I just put the entire context in request
Answer
Ring-necked DuckOP
downgrading to next 13.2.4 just fixed a very weird af bug
I was setting res.locals in express before passing to the nextApp handler, but ctx.res.locals showed up as undefined in getServerSide props. Downgrading fixed it.
success!
put everything you want to maintain reference wise between nextjs and express in res.locals, and call it in next using ctx.res.locals[variable]
Northeast Congo Lion
nice
so yeah you just passing it in the req/res right?
Ring-necked DuckOP
Yes
Tomistoma
Hey @Ring-necked Duck , I hope you don't mind me asking this here. Almost the same problem, but I am instead struggling with env variables
How can I use custom express js in next.js(as you did, with next.prepare() ) and succeed in reading environment variables from the same place? I came across the .env.local thingy, it miserably fails.
You can’t share variables in that sense between express js and nextjs. They are different applications with separate contexts.
Tomistoma
The documents tend to claim a different thing. Check out the environment variables - section in their documentation (Next.js 's)
However I am not able to achieve the desired outcome
@Northeast Congo Lion I think the solution achieved was to pass values using request body
Tomistoma
Yes but wasn't it a different subject? The OP wanted to achive sharing I believe not env but run time variables... correct me if I am wrong
Northeast Congo Lion
Correct.
OP wanted to use variables from nextjs on express
So maybe in next serverisde req.locals is accessible?