javascript - Nuxt server middleware post request not working - Stack Overflow

I saw in the documentation of Nuxt js (.xconfiguration-glossaryconfiguration-servermiddleware) that

I saw in the documentation of Nuxt js (.x/configuration-glossary/configuration-servermiddleware/) that I can extend server middleware with express. i tested it with GET request and it worked but when i use POST request there is no body in request.

/api/index.js:

const express = require('express');
const bodyParser = require('body-parser');
import Cities from './offline/cities';

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/findCity', function (req, res) {
  if (!req.body.input) {
    res.status(400).json();
    return;
  }
  res.status(200).json(Cities.filter(req.body.input, req.body.opt));
});

module.exports = { path: '/api', handler: app };

/nuxt.config.js:

serverMiddleware: [ '~/api/index.js' ],

mixin.js

async findCity(input, opt) {
   return (await this.$axios.post(process.env.DOMAIN_URL + '/api/findCity', { input, opt })).data;
}

in chrome dev tools the body is sent:

I saw in the documentation of Nuxt js (https://nuxtjs/docs/2.x/configuration-glossary/configuration-servermiddleware/) that I can extend server middleware with express. i tested it with GET request and it worked but when i use POST request there is no body in request.

/api/index.js:

const express = require('express');
const bodyParser = require('body-parser');
import Cities from './offline/cities';

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/findCity', function (req, res) {
  if (!req.body.input) {
    res.status(400).json();
    return;
  }
  res.status(200).json(Cities.filter(req.body.input, req.body.opt));
});

module.exports = { path: '/api', handler: app };

/nuxt.config.js:

serverMiddleware: [ '~/api/index.js' ],

mixin.js

async findCity(input, opt) {
   return (await this.$axios.post(process.env.DOMAIN_URL + '/api/findCity', { input, opt })).data;
}

in chrome dev tools the body is sent:

Share Improve this question edited Mar 8, 2022 at 11:25 Arshia Moghaddam asked Jan 13, 2021 at 7:16 Arshia MoghaddamArshia Moghaddam 5388 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Exclude body-parser

I don't know if this would directly solve the original problem, but it is an efficiency. This helped me when troubleshooting getting req.body.

With Express +4.16, you don't need a separate import for body-parser because .json and .urlencoded are included in Express base.

Changelog: https://expressjs./en/changelog/4x.html#4.16.0

An article about it: https://medium./@mmajdanski/express-body-parser-and-why-may-not-need-it-335803cd048c

This works for me:

const express = require('express')
const app = express()
app.use(express.urlencoded({extended: true}))
app.use(express.json())

app.all('/myAPIroute*', (req, res) => {
    console.log("Req body is: ", req.body)
})

module.exports = app

In your /api/index.js file you can get request data by this way:

req.on('data', (data) => {
    console.log(data.toString())
})

Using only data you get a readable Buffer object. Adding .toString() you have a 'plain text' representation of data.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745651851a4638312.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信