Suchitra Iyer Suchitra Iyer - 1 year ago 64
Node.js Question

access variables through request body in node js express

I am new to node js. I want to access the value of a variable that is in a HTML page through node js express.

My HTML side code written within the script tags is

url: '/modulename',
type: 'POST',
dataType: 'JSON',
data: JSON.stringify([

success: onMemberSucces,
error: onMemberError

The packages I am using for my js application are

"dependencies": {
"all": "0.0.0",
"body-parser": "^1.15.1",
"cookie-parser": "^1.4.2",
"d3": "^3.5.17",
"express": "^4.13.4",
"jquery": "^2.2.4",
"jsdom": "^9.2.1",
"mssql": "^3.3.0",
"node-sqlserver-unofficial": "1.4.0",
"require": "^2.4.20",
"requirejs": "^2.2.0"

The code on my js application is

app.use(express.static('D:/d3 project/project_part1/project_part1/'));
var server = app.listen(8081);'/modulename', function (req, res, body) {
modulename = req.body;
console.log("hi \n"+ modulename);
console.log("hi "+ modulename.module);


the output on the console is

hi [object Object]
hi undefined

I have tried to print

This also prints undefined at the console. I am doing a very careless silly mistake,but I am not able to figure it out and have spent quite a few hours on it. Please help me.Thanks in advance.

Answer Source

Change the data to

data: {module:"abc"},

Jquery will take care of the rest, in your node route


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download