question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Example with API routes handling POST requests with a custom Express server

See original GitHub issue

Can you please add an example here:

https://github.com/zeit/next.js/tree/master/examples

that uses API routes to handle POST requests with also a custom Express server to serve the application? I feel like there are conflicts between routes provided by the Express server, the file system routing of Next and the API routes within the api folder. The following are related issues:

https://github.com/zeit/next.js/issues/7960

https://github.com/zeit/next.js/issues/8178

https://github.com/zeit/next.js/issues/8120

In order to make it work now I have to do something that seems pretty crazy:

pages/api/signin.js

'use strict';
module.exports = require('../../src/apiHandlers').postSignIn;

server.js

expressApp.post('/api/signin', require('./pages/api/signin'))

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

67reactions
ivan-kleshnincommented, Aug 7, 2019

Sorry for interfering… But why do you people need API routes? Having a separate server that handles API is more performant, more stable, more extensible and more comfortable to configure. You will be able to deploy them separately as well. No idea why someone decided to add this feature…

6reactions
peeccommented, Sep 11, 2019

I am also missing a way to have both, api routes and a custom server.js. The api routes are ignored once you use a custom server.js. The reason why I use a custom server.js currently is only to force SSL redirection in production mode. I know some people would recommend handling this in a load balancer - but there is no way to do this on a standard heroku instance.

My workaround is basically not using API routes and do everything with express.

I have not tried, but maybe it does not work because there is no handler for POST in the examples…

so maybe something like this would work and if it does work, examples should probably be updated:

app.prepare().then(() => {
  const server = express()
  // ... custom express middlewares

  server.get('*', (req, res) => {
    return handle(req, res)
  })
  // Does this fix it? Also fallback for POST.. ?
  server.post('*', (req, res) => {
    return handle(req, res)
  })

  server.listen(port, err => {
    if (err) throw err
    console.log(`> Ready on http://localhost:${port}`)
  })
})

Read more comments on GitHub >

github_iconTop Results From Across the Web

Routing - Express.js
Routing. Routing refers to how an application's endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing.
Read more >
How to create a REST API with Express.js in Node.js
A Node.js with Express tutorial to learn how to create a REST API for CRUD operations which can be consumed by a client...
Read more >
Handle GET and POST Request in Express - CodeForGeek
GET and POST is two common HTTP Requests used for building REST APIs. Both of these calls are meant for some special purpose....
Read more >
CRUD REST API with Node.js, Express, and PostgreSQL
Our goal is to allow CRUD operations, GET , POST , PUT , and DELETE , on the API, which will run the...
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
First we create routes for a wiki in a module named wiki.js. The code first imports the Express application object, uses it to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found