简体   繁体   中英

nodejs express data form

I'm writing a web server, and trying to solve a problem which annoyed me half of a day.

The problem is, when I'm going to throw a data-set to node-express's routes, I checked that the data-set which I sent from web page is empty in routes!

I'm so confused about this issue, cause I setted all of the environment settings that recommend in google.

Here is my core code about the problem:

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); 

        <form id="myform" action="http://localhost:5000/file_upload/<%=user_id%>" method='POST' enctype="multipart/form-data">
            <input type="file" name="file[]" multiple="multiple" accept=".stl"/>
            <input type="text" name="id_idx" placeholder="id indx"/> 
            <input type="text" name="folder_name" placeholder="folder name"/> 
            <input type="submit"/>
        </form>

Although I set the settings like those, when I going to use the data like "req.query" or "req.body". It shows a empty set.

How could I figure it out?

Thank you!

as I am looking here you are using form data to send to the backend.

body-parser doesn't work or handle multipart bodies, which is what FormData is submitted as.

you can try to use a module like multer

var express = require('express')
var app = express()
var multer  = require('multer')
var upload = multer()
 
app.post('/', upload.none(), function (req, res, next) {
  // req.body contains the text fields
     const formData = req.body;
     console.log('form data', formData);
})

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