There is the following form:

<form id="form" action="/" method="post"> <input type="text" id="title"> <input type="text" id="data"> <input type="submit" id="submit" value="ΠΎΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ"> </form> 

and server.js file:

 var express = require("express"); var bodyParser = require('body-parser'); var multer = require('multer'); // v1.0.5 var upload = multer(); // for parsing multipart/form-data var app = express(); app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded app.set("view engine","ejs"); app.use(express.static(__dirname + "/public")); app.get("/", function(req,res){ res.render("pages/index", { title: "Главная", msg: "ΠŸΡ€ΠΈΠ²Π΅Ρ‚ ΠœΠΈΡ€!", data: q, info: "" }); }); app.post('/', upload.array(), function (req, res, next) { console.log(req.body); //{} console.log(req.body.title); //undefined console.log(req.body.data); //undefined res.json(req.body); //{} }); app.get("/hello", function(req,res){ res.render("pages/other", { title: "Другая страница", msg: "hello world" }); }); app.use(function(req, res, next) { res.status(404).render("pages/error", { title: "ошибка 404", msg: "ошибка 404" }); res.status(500).render("pages/error", { title: "ошибка 500", msg: "ошибка 500" }); }); app.listen(8080); console.log("Π‘Π΅Ρ€Π²Π΅Ρ€ Π·Π°ΠΏΡƒΡ‰Π΅Π½!"); 

I do it by tutorials, I recently started to deal with this, I came across the fact that I created the form, but I cannot receive data on the server in any way. tried both req.query and req.params ('title') - undefined.

Packages installed that connect.

Actually ... a lot of information in English - maybe I'm missing something?

Question: How to get data from the form transmitted by the post method and process it on the server using nodejs and express.js technologies?

    1 answer 1

    To submit form data, you must name the fields.

     <form id="form" action="/" method="post"> <input type="text" name="title" id="title"> <input type="text" name="data" id="data"> <input type="submit" id="submit" value="ΠΎΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ"> </form> 

    In name - with what name the parameter will arrive to the server. id - inputa id for the browser, is not connected to the server

    • one
      app.use(function(req, res, next) { res.status(404).render("pages/error", { title: "ошибка 404", msg: "ошибка 404" }); res.status(500).render("pages/error", { title: "ошибка 500", msg: "ошибка 500" }); }); leave one res.status () handler ... otherwise when you get to this place, you risk putting an application - NumminorihSF
    • In general, for such situations it is better to check whether something is sent from the browser. This can be done through the developer tools that are in any browser (honestly, I don’t know about IE, most likely you also have it already), in the section on working with the network. - NumminorihSF
    • Thank you very much! It worked! ... Yes, I suspected that the matter was in the name, but some obsolete feature of working with the tags "a" and anchors where the name is no longer used is stuck in the memory. - silksofthesoul