vendredi 29 janvier 2021

ExpressJs : Get data of Form data body

I use express js to create api.

In Postman I pass:

POST http://localhost:3000/test

Form data :     email: 'nasd@gmail.com'

header :     Content-Type: 'application/x-www-form-urlencoded'

when I console.log(req.body)

It appear:

{ '------WebKitFormBoundary9RYiOTi2ZDqN1AnB\r\nContent-Disposition: form-data; name': '"email"\r\n\r\nasd@gmail.com\r\n------WebKitFormBoundary9RYiOTi2ZDqN1AnB--\r\n' }

How can I get the email field?

This is my Code:

let express = require("express");
let morgan = require("morgan");
let bodyParser = require("body-parser");
let expressValidator = require("express-validator");
let session = require("express-session");
var multer = require("multer");
var upload = multer();
let MySQLStore = require("express-mysql-session")(session);

let app = express();
let PORT = 3000;

let options = {
  host: "localhost",
  port: "3306",
  user: "root",
  password: "12345678",
  database: "timekeeper4",
};
let sessionStore = new MySQLStore(options);

app.use(
  session({
    key: "session_cookie_name",
    secret: "session_cookie_secret",
    store: sessionStore,
    resave: true,
    saveUninitialized: false,
  })
);
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(expressValidator());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(upload.array());
app.use(express.static("public"));

app.use("/test", (req, res) => {
  console.log(req.body.name);
  console.log(req.body);
  res.send("Hello World!");
});

app.listen(PORT, () => {
  console.log("Server started on http://localhost:" + PORT);
});

module.exports = app;



Aucun commentaire:

Enregistrer un commentaire