[SOLVED] Next.js App Router with Express Custom Server

Hi Theo, I watched your YouTube video on App Router Review six months in, great video, thanks for posting. Quick question. I am looking for a project template / example that uses Next.js App Router (v.13.4.7) with a custom Express.js server backend, and can't seem to find one. I've tried ChatGPT, googling GitHub repos, etc., and can't find an example. Basically, I am trying to get a custom Express.js server to work with the Next.js App Router, but can't seem to get it to work in my project -- I keep getting a ECONREFUSED error. Can you point me to an example?
3 Replies
jelugbad
jelugbad12mo ago
UDPATE: I just confirmed. The Express custom router works with the Next.js Pages Router. Have not been able to get it to work with the Next.js App Router.
bakdaddy
bakdaddy12mo ago
do you want to use it with something premade? or what do you want to use it for? maybe doing a separate express server would be better?
jelugbad
jelugbad12mo ago
I finally got it to work. I spent 2 days trying to figure this out.... The mistake I was making was that I was not passing hostname and port when declaring the next app in the custom Express server.js file, as in const app = next({ dev, hostname, port }) I was just passing "dev". Also, I had to define hostname like so: const hostname = '127.0.0.1' Declaring it as they provide in the docs, like so const hostname = 'localhost' did not work for me. What caused the confusion is the custom server example they provide in the Next.js docs are listed under the "Pages Router", not "App Router".