Example with API routes handling POST requests with a custom Express server
See original GitHub issueCan 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:
- Created 4 years ago
- Reactions:7
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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…
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: