0

im trying to create a simple webpage with some server side functions but somehow 2 things arent working as they supposed to work.

via html button im running a clientside javascript that does a http post request.
Client side javascript

httpRequest = new XMLHttpRequest()
httpRequest.open('POST', '/test2')
httpRequest.send(var1,var2,var3,var4);

Server.js

var express = require('express');
var bodyParser = require("body-parser");
var dbFunc = require("./dbFunctions.js");
var app = express();
var path = require('path');
var port = 8888;
//allow to use body-parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//allow to use static files
app.use(express.static("public"));
//listen to smth
app.post('/test2', function (req, res) {
console.log("worked");
});
//start server

app.listen(port);
console.log("Server running on port" + port);

My server detects this post http request and does the "console.log" but how do i get the parameters from the http.request as a variable? I tried to work with bodyParser but somehow my object is always empty.

Another thing is, ive created another Javascript file(dbFunctions.js) and also implemented it in the server file but if i try to run a function(dbFunc.test("hello") for example) it says "dbFunc.test is not a function".

dbFunctions.js

function DBFunctions(){
    function test(a){
        console.log(a);
    }
}

Ive also tried to do something like this, but this gave me the same error.

function test(a){
console.log(a);
}

Could someone give me a hint or tell me what i am missing?

Puneet
  • 470
  • 5
  • 17
Shiroyashaxdd
  • 93
  • 1
  • 8
  • 1
    with `HMLHttpRequest.send()` you need to make sure you pass in what that function expects, it doesn't expect multiple arguments. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send – George Mar 28 '18 at 10:42
  • Possible duplicate of [How are parameters sent in an HTTP POST request?](https://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request) – Suhail Gupta Mar 28 '18 at 10:48
  • `test` inside `DBFunctions` cannot be accessed by `test.DBFunctions` – George Mar 28 '18 at 10:52

2 Answers2

1

Answer1:

The way you are sending data into post request is wrong, you slould send in format of, xhttp.send("val1=val1&val2=val2");

httpRequest = new XMLHttpRequest()
httpRequest.open('POST', '/test2')
httpRequest.send(var1=var1&var2=var2&var3=var2&var3=var3);

To POST data like an HTML form, add an HTTP header with setRequestHeader(). Specify the data you want to send in the send() method:

httpRequest = new XMLHttpRequest()
httpRequest.open('POST', '/test2')
httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpRequest.send(var1=var1&var2=var2&var3=var2&var3=var3);

In your server.js, use req.body to get those values.

app.post('/test2', function (req, res) {
    console.log("worked");
    console.log(req.body)
  });

XMLHttp request example

Answer2:

From your dbFunctions.js file you should export your function using module exports in node js.

dbFunctions.js:

var exports = module.exports = {};

exports.test = function(a) {
  console.log(a);
};

You can also do as,

module.exports = {
  test: function(a) {
    console.log(a)
  },
}

Now in your server.js

var dbFunc = require("./dbFunctions.js");

dbFunc.test();

Node js module exports

Sravan
  • 16,897
  • 2
  • 24
  • 48
0

All your posted variables will be available in req.body try console.log(req.body).

Secondly you most probably not exporting test function from dbFunction.js, that's why getting "dbFunc.test is not a function" because dbFunc.test is undefined.

Note: As mentioned in one of the comment, you need to use httpRequest.send(var1,var2,var3,var4); in the right way. It does not expect multiple parameters.

Like: httpRequest.send("var=var1&var1=var2")

Please refer: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send

Farhan Tahir
  • 1,956
  • 1
  • 10
  • 23