Is it possible to send post data to nextjs route? (now 2.0)
See original GitHub issueI want my nextjs to recieve post form and then it can pass data to nextjs route. I don’t want to send data via query params for some reason.
server.post('/products', (req, res) => {
// send post form with x-www-form-urlencoded
return app.render(req, res, '/product', { payload: req.query.name })
})
Product.getInitialProps = props => {
const { payload } = props.query
return { payload }
}
function Product(props) {
return <p>{props.payload}</p>
}
I can make it work when I use expressjs and run on my local server.
But when I try to deploy to now 2.0 following this https://zeit.co/guides/custom-next-js-server-to-routes/
I’m not sure how can I pass my post data form to nextjs route.
{
"version": 2,
"routes": [
{ "src": "/product", "method": "POST" ,"dest": "/product?payload=???? }
]
"builds": [
{
"src": "next.config.js",
"use": "@now/next"
},
{ "src": "index.js", "use": "@now/node" },
{
"src": "static/*",
"use": "@now/static"
}
]
}
How can I send payload data that came from post form (x-www-form-urlencoded) to nextjs routes?
I try this in my index.js but it does not work
'use strict'
const express = require('express')
const helmet = require('helmet')
const app = express()
app.use(helmet())
app.use(express.urlencoded({ extended: true }))
app.post('/product', (req, res) => {
console.log('body', req.body)
res.status(200).send(req.body)
})
module.exports = app
I can get req.body but I cannot pass it to the nextjs route.
Is there anyway that lamdas can do something like this?
server.post('/', (req, res) => {
console.log('payload server', req.body)
return app.render(req, res, '/', { payload: req.body })
})
System information
- Version of Next.js: [e.g. 8.1]
thanks for helping 😃
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:14 (5 by maintainers)
Top Results From Across the Web
how to handle a post request in next.js? - Stack Overflow
To get POST requests working in Next.js API routes, you likely want to do 3 things. Limit the method to POST; Use JSON.parse()...
Read more >Data Fetching: getStaticPaths - Next.js
When exporting a function called getStaticPaths from a page that uses Dynamic Routes, Next.js will statically pre-render all the paths specified by ...
Read more >How to use Next JS to send POST requests to Salesforce ...
Show activity on this post. The console.log(data) statement in your API route simply prints to stdout — it won't actually send a response....
Read more >The Next.js Handbook - The Valley of Code
We'll see how to provide the data to the component with getInitialProps in the next lesson. For now, add a little if (!post)...
Read more >The Ultimate Guide to Next.js Authentication with Auth0
Learn how and where to authenticate your user in the different deployment models that exist for Next.js. This guide explores the Custom ...
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 Free
Top 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

I was able to get the post form data using the package
urlencoded-body-parserand parse the data from the req object inside thegetInitialPropsfunction.@huv1k I wanna use Nextjs as the replacement for PHP! It should support that!