简体   繁体   中英

Handling api requests via next.js routing

This is a next.js handler function for dealing with api requests on our server i need to access a variable between request methods any help will be muchly appreciated.

export default function handler(req, res) {
    if(req.method ===  'POST') {
        var hi = req.body
    }
    if (req.method === 'GET') {
        console.log(hi)
    }
}

Just define it outside the if statements like this:

export default function handler(req, res) {
    var hi = req.body
    if(req.method ===  'POST') {
        console.log(hi)
    }
    if (req.method === 'GET') {
        console.log(hi)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM