Please tell me why I can not connect and use express.bodyParser? How can I do this?
app.use (express.static (__ dirname + '/ dist'));
app.use (todoRoutes);
function start () {
Try {
app.listen (PORT, () = & gt; {
console.log ( "Server has been statred ...")
});
}
catch (e) {
console.log (e);
}
}
start ();
const {Router} = require ( 'express');
const router = Router ();
var foo = require ( "../ script.js");
router.configure (function () {
router.use (express.bodyParser ());
});
const path = require ( 'path');
var myModule = require ( '../ index.js');
router.get ( '/', (req, res) = & gt; {
res.sendFile (path.resolve ( './ dist / index.html'));
});
router.post ( '/ 1', (req, res) = & gt; {// sending the first screen
console.log ( "POST");
res.sendFile (path.resolve ( './ data / firstScreen.json'));
})
router.post ( '/ 2', (req, res) = & gt; {// sending the second screen
console.log ( "POST");
res.sendFile (path.resolve ( './ data / secondScreen.json'));
})
router.post ( '/ currentDir1', (req, res) = & gt; {
console.log ( "POST");
let body = "";
let pathToFile = "";
console.log (req.body);
req.on ( "data", function (data) {
body + = data;
});
req.on ( "end", function (currentData) {
console.log (JSON.parse (body));
currentData = JSON.parse (body);
if (currentData.sizeOrType === "& lt; folder & gt;") {
let dir = currentData.dir + currentData.fileName;
// dir = "C: \\ totalcmd";
console.log (dir);
if (currentData.whichScreen) {
console.log (currentData.whichScreen);
console.log ( "ggg");
foo (dir, './data/firstScreen.json');
pathToFile = './data/firstScreen.json';
res.sendFile (path.resolve ( './ data / firstScreen.json'));
} Else {
console.log ( 'aaaa');
foo (dir, './data/secondScreen.json');
pathToFile = './data/firstScreen.json';
res.sendFile (path.resolve ( './ data / secondScreen.json'));
}
}
// // res.json ({message: 'goodbye'})
res.sendFile (path.resolve ( './ data / firstScreen.json'));
});
// res.sendFile (path.resolve (pathToFile));
})
module.exports = router;
Answer 1, Authority 100%
If you look at documentation
There are several examples of the use
example of the Express / Connect top-level generic
var express = require ( 'express')
var bodyParser = require ( 'body-parser')
var app = express ()
// parse application / x-www-form-urlencoded
app.use (bodyParser.urlencoded ({extended: false}))
// parse application / json
app.use (bodyParser.json ())
app.use (function (req, res) {
res.setHeader ( 'Content-Type', 'text / plain')
res.write ( 'you posted: \ n')
res.end (JSON.stringify (req.body, null, 2))
})
Express route-specific
var express = require ( 'express')
var bodyParser = require ( 'body-parser')
var app = express ()
// create application / json parser
var jsonParser = bodyParser.json ()
// create application / x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded ({extended: false})
// POST / login gets urlencoded bodies
app.post ( '/ login', urlencodedParser, function (req, res) {
res.send ( 'welcome,' + req.body.username)
})
// POST / api / users gets JSON bodies
app.post ( '/ api / users', jsonParser, function (req, res) {
// create user in req.body
})
I think you will start something like this
const {router} = require ('express');
Const Router = Router ();
Var Foo = Require ("../ script.js");
Var Bodyparser = Require ('Body-Parser')
//router.configure(Function () {
// Router.use (express.bodyparser ());
//});
// Parse Application / Json
Router.use (bodyparser.json ())
...