vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.04k stars 27k forks source link

Custom server not working #9397

Closed msukmanowsky closed 4 years ago

msukmanowsky commented 5 years ago

Bug report

Describe the bug

Following the docs for custom server and routing, a simple server.js example runs locally but does not work when deploying to zeit.co.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Clone https://github.com/msukmanowsky/zeit-test and install dependencies npm install
  2. Run npm run dev and navigate to http://localhost:3000/a (should show JSON response)
  3. Deploy to Zeit via npm run deploy
  4. Access https://yoursite.now.sh/a and you'll see you get a 404 error, not the correct JSON response

Expected behavior

Custom route /a should return JSON when deployed to zeit.co but it shows a 404 error.

System information

jamesmosier commented 5 years ago

You could try using Next's API routes instead to serve a JSON response from a specified url.

Otherwise, Zeit Now has some configuration options for Routes: https://zeit.co/docs/configuration/#routes

{
  "routes": [{ "src": "/a" }]
}
msukmanowsky commented 5 years ago

Thanks @jamesmosier. API routes won't work for my use case (which by the way is a Shopify app).

Shopify adds some custom OAuth routes and a GraphQL proxy endpoint.

Tried adding that config but still have the same issue. Navigating to https://zeit-test.mikesukmanowsky.now.sh/a produces a 404 error.

jamesmosier commented 5 years ago

I did some more thinking and you can combine the API routes and Now routing.

  1. Create your API route under pages/api/a.js
  2. Update your now.json to point to this API route:
    {
    "version": 2,
    "routes": [
        { "src": "/a", "dest": "/api/a" }
    ]
    }
msukmanowsky commented 4 years ago

@jamesmosier that does work, but the issue I have is that I need to use some Koa middleware and you can't really use those with the pages/api based approach.

jamesmosier commented 4 years ago

Ahhh I see. Then from the resources I found, a custom server and Now won't work together:

https://spectrum.chat/zeit/now/how-to-deploy-next-js-app-with-custom-express-server-to-now-v2~ab4b1807-b516-41f5-a4ec-986036212c14?m=MTU1NjY0MTQzNjk3Ng==

https://spectrum.chat/next-js/general/deploying-next-js-app-custom-express-server-to-now-2-0-serverless~7e77107a-a799-4ff2-a813-8eb85fd0ad43

https://spectrum.chat/zeit/now/custom-express-server-next-js-example~f4c2214d-03d1-4df6-9a9a-e3346c7b9bec

All the recommendations I saw were when needing 1 off API endpoints with Next & Now, either use the API routes and now.json routing or create pages that handle this in getInitialProps, both of which won't work if you need a more complex server setup with Koa.

Timer commented 4 years ago

Custom servers do not work on ZEIT Now. You'll need to use our built-in router and potentially the upcoming Custom Routes RFC: #9081.

We'll be removing most of the custom server examples soon-ish. They shouldn't be needed for most applications!

balazsorban44 commented 2 years ago

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.