无法在节点中使用表单数据插入数据

时间:2018-08-15 08:06:03

标签: node.js postman form-data

以下是使用Node和mongoDB创建的登录API的代码示例。 usernamepassword是要解析以登录登录API的两个参数。但是usernamepassword仅由Postman Body x-www-form-urlencoded允许,但我想使用form-data。当我通过表单数据插入username时,邮递员会显示

{
    "msg": "Invalid request, Invalid 'username' parameter.",
    "status": "Failed"
}

AuthController.js

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

const multer = require('multer');
const storage = multer.diskStorage({});
const upload = multer({storage: storage});

var VerifyToken = require('./VerifyToken');

var User = require('../user/User');

router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());

/**
 * Configure JWT
 */
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var bcrypt = require('bcryptjs');
var config = require('../config'); // get config file
router.post('/login', function(req, res)
{
  postLogName = req.body.username
  postLogPass = req.body.password

  console.log(postLogName);

  if(!postLogName)
  {
      return res.status(400).send(
      {
          "msg": "Invalid request, Invalid 'username' parameter.",
          "status": "Failed"
      });
  } 
  if(!postLogPass)
  {
      return res.status(400).send(
      {
          "msg": "Invalid request, Invalid 'password' parameter.",
          "status": "Failed"
      });
  }
  console.log(postLogName);

  User.findOne({ username: req.body.username }, function (err, user) {
    if (err) return res.status(500).send('Error on the server.');
    if (!user) return res.status(404).send('No user found.');

    // check if the password is valid
    var passwordIsValid = bcrypt.compareSync(req.body.password, user.password);
    if (!passwordIsValid) return res.status(401).send({ auth: false, token: null });

    // if user is found and password is valid
    // create a token
    var token = jwt.sign({ id: user._id }, config.secret, {
      expiresIn: 86400 // expires in 24 hours
    });

    // return the information including token as JSON
    res.status(200).send({ auth: true, token: token });
  });
});

如何使用表单数据插入?

已编辑

当我更改为"msg" : req时,我尝试使用表单数据时遇到以下错误。

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at stringify (C:\userAPI\node_modules\express\lib\response.js:1119:12)
    at ServerResponse.json (C:\userAPI\node_modules\express\lib\response.js:260:14)
    at ServerResponse.send (C:\userAPI\node_modules\express\lib\response.js:158:21)
    at router.post (C:\userAPI\auth\AuthController.js:32:32)
    at Layer.handle [as handle_request] (C:\userAPI\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\userAPI\node_modules\express\lib\router\route.js:137:13)
    at multerMiddleware (C:\userAPI\node_modules\multer\lib\make-middleware.js:18:41)
    at Layer.handle [as handle_request] (C:\userAPI\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\userAPI\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\userAPI\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\userAPI\node_modules\express\lib\router\layer.js:95:5)
    at C:\userAPI\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\userAPI\node_modules\express\lib\router\index.js:335:12)
    at next (C:\userAPI\node_modules\express\lib\router\index.js:275:10)
    at jsonParser (C:\userAPI\node_modules\body-parser\lib\types\json.js:101:7)

1 个答案:

答案 0 :(得分:0)

1)在需要的时候加木工,根据您的代码,我看不到您在哪里使用它们:

const multer = require('multer');
const storage = multer.diskStorage({});
const upload = multer({storage: storage});


2)在一般级别(在app.js中)附加正文解析器,因此也请删除这些行:

var bodyParser = require('body-parser');

router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());


3)在您定义了快速应用的app.js中,附上以下内容:

const bodyParser = require('body-parser');
const formidable = require('formidable'); // installation: npm i --save formidable
const multipartForm = new formidable.IncomingForm();

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());


// let's attach our custom middleware
// which will check headers for content-type
// and parse request body and 
// assign body to request context (req object)
// and call next to pass request handling
// to next handlers
app.use((req, res, next) => {
  if (req.headers['content-type'].indexOf('multipart/form-data') > -1) {
    multipartForm
      .parse(req, (error, body, files) => {
        // same as:
        // req.body = body;
        // req.files = files;
        Object.assign(req, {body, files});
        next();
      });
    return;
  }
  next();
});

所以我已经测试了以下代码:

'use strict';

const express = require('express');
const app = express();

const bodyParser = require('body-parser');
const formidable = require('formidable');
const multipartForm = new formidable.IncomingForm();

app.use(bodyParser.urlencoded());
app.use(bodyParser.json());

app.use((req, res, next) => {
  if (req.headers['content-type'].indexOf('multipart/form-data') > -1) {
    multipartForm
      .parse(req, (error, body, files) => {
        Object.assign(req, {body, files});
        next();
      });
    return;
  }
  next();
});

app.post('/data', (req, res) => {
  const body = req.body;
  res.status(200).send({body});

});

app.listen(3000);

它按预期工作:

Urlencoded url encode form

表单数据 mutipart data form

p.s。您可以阅读有关编写中间件here

的信息。