Next.js Discord

Discord Forum

is there a way to serve already build next js websites using express

Unanswered
Minskin posted this in #help-forum
Open in Discord
Avatar
MinskinOP
I want to serve a nextjs website on a subdomain of a express server

34 Replies

Avatar
Ping me for help
You want to host it in a subdomain. You can see a guide on google about using custom servers. Note: Nextjs servers are optimized, and you might have to optimize custom servers.
Avatar
MinskinOP
Can you please give me a example
I couldn't get anything to work
I am too dumb
Avatar
Ping me for help
I'm unable to understand what you exactly need. are you pointing your subdomain to same server?
Avatar
MinskinOP
I am just using some middleware to check if a hostname startswith something and forward to request to something else
I wanna forward the request to a next js server thingy
Avatar
Ping me for help
Avatar
MinskinOP
This doesn't work on built next apps
This requires a page and apps directory which would be found in uh
Some folder deep in the building directory
Avatar
Ping me for help
Have you tried it out?
Avatar
MinskinOP
Yes
Avatar
Ping me for help
Can you share any screenshot about the error or anything
Avatar
MinskinOP
Aight wait
Avatar
MinskinOP
$ tsx src/main.ts
/home/aj/builder-server/node_modules/next/dist/lib/find-pages-dir.js:42
        throw new Error("> Couldn't find any `pages` or `app` directory. Please create one under the project root");
              ^

Error: > Couldn't find any `pages` or `app` directory. Please create one under the project root
    at findPagesDir (/home/aj/builder-server/node_modules/next/dist/lib/find-pages-dir.js:42:15)
    at initialize (/home/aj/builder-server/node_modules/next/dist/server/lib/router-server.js:71:69)
    at async NextCustomServer.prepare (/home/aj/builder-server/node_modules/next/dist/server/next.js:240:28)
@Ping me for help
Avatar
Ping me for help
So i guess your only option is to copy the app folder into the product root. Or you can run the next server separately the proxy then request
Avatar
MinskinOP
how can i do the latter
also the app folder doesnt contain the static files
css and images and shit
Avatar
Ping me for help
Proxy? It’s difficult to explain, but I can give you code in a few hours
Avatar
MinskinOP
i think i got it to run, but
Avatar
Ping me for help
You run the next app, rewrite or do something similar to the requests
Avatar
MinskinOP
yarn next start serves on a different port each time
thats the main problem
Avatar
Ping me for help
You can specify a port
And how about you use nginx
To forward request from a specific domain to next js and other to express server
Avatar
MinskinOP
cant i just not use ports and just forward requests to the server
the next server
Avatar
Ping me for help
You can do that using nginx
Avatar
MinskinOP
i have never used nginx
Avatar
Ping me for help
You can learn to use it then