Express js form data

后端 未结 4 2008
难免孤独
难免孤独 2020-12-02 12:53

Can someone please tell me the recommended (up to date) way to get POSTed form data in express.

So many tutorials/ posts etc talk about bodyParser but this is no lon

相关标签:
4条回答
  • 2020-12-02 13:19

    You should install body-parser through npm-install. Now it comes as a separate middleware.

    After that add following line in your app.js

    var bodyParser = require('body-parser');
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded());
    // in latest body-parser use like below.
    app.use(bodyParser.urlencoded({ extended: true }));
    

    It parses the post request as an object. You will get your variables in req.body.

    In your post request handler.

    app.post('/post',function(request,response){
       console.log(request.body) //you will get your data in this as object.
    })
    

    Edit 1

    The answer above was for the question specifically asked, the OP was looking for the bodyParser(deprecated) which was not part of express anymore.

    Since the title of the question is very generic and the answer doesn't include all aspects of form-data, I will put @StLia's answer as an edit.

    Body-Parser Readme

    This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:

    • busboy and connect-busboy
    • multiparty and connect-multiparty
    • formidable
    • multer
    0 讨论(0)
  • 2020-12-02 13:22

    Besides the solutions with formidable, there is another module which I have been using in my recent projects since 2019. The module express-form-data can be easily declared in your server file like:

    const express = require('express');
    const formData = require('express-form-data');
    
    app.use(formData.parse());
    
    app.post('/image-upload', (req, res) => {
      console.log(req.files);
    })
    
    ...
    

    In case of image uploading, for instance, req.files will provide all relevant data you need for handling the files such as path, size, filename, etc.

    0 讨论(0)
  • 2020-12-02 13:25

    From the README of body-parser:

    This does not handle multipart bodies, due to their complex and typically large nature.

    The above is going to work with x-www-form-urlencoded and json but it will NOT work with any multipart. form-data is also multipart with the header multipart/form-data.

    In case of form-data, your best solution would be to use express-formidable.

    0 讨论(0)
  • 2020-12-02 13:37

    You can make use of express-formidable module to that. install 'express-formidable' by the following command npm install express-formidable

    the simple example is as follows

    const express = require('express');
    const formidable = require('express-formidable');
    
    var app = express();
    
    app.use(formidable());
    
    app.post('/upload', (req, res) => {
      //req.fields contains non-file fields 
      //req.files contains files 
      res.send(JSON.stringify(req.fields));
    });
    

    Click here for further description

    0 讨论(0)
提交回复
热议问题