purxiz
purxiz

Reputation: 103

Nodejs server with Express not sending responses to HTTP requests from javascript file, but is receiving the requests and processing them

So I have a node-js server and an apache server on the same machine, and one of the javascript files is sending an HTTP request to the node-js server. The node-js server receives the file, reads the data, puts it in the database, as it should, but it isn't sending back any status codes or data.

Here is the XHTMLRequest send code snippet,

// creates a new http request to be sent to the nodejs server
function createNewUser(username, password, email) {

    // The url is the URL of our local nodejs server
    var userCreateRequest = new XMLHttpRequest();
    userCreateRequest.open( "POST", "http://<machine's IP>:8080/api/users" );

    // Create json object for user data
    var user = "name="+username+"&password="+password+"&email="+email;
    alert(user);

    // set content type for http request
    userCreateRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    // Event listern for server response
    // userCreateRequest.addEventListener("readystatechange", processRequest, false);

    // Call process request whenever state changes
    userCreateRequest.onreadystatechange = function() {
            alert(this.readyState + ", " + this.status);
            if (this.readyState == 4 && this.status == 200) {

                    var response = this.response;
                    alert(response.name);

            }

    }

    // Send user data to server
    userCreateRequest.send(user);

}

And here is the code for the node-js server (with express)

router.route('/users')

    .post(function(req, res) { //create a new user

            var user = new User();

            user.name = req.body.name;
            user.password = req.body.password;
            user.email = req.body.email;

            user.save(function(err) { //add user object to database

                    if(err)
                            res.send(err);

                    res.status(200).json(user);

            });

    });

As I said above, the code works fine in terms of putting the body of the request in the database and what-not, but the server is not sending back the 200 OK response (or I'm failing to receive it for some reason). The only times I get an alert from onreadystatechange is when it's state 2, status 0, and state 4, status 0.

Upvotes: 2

Views: 6053

Answers (2)

purxiz
purxiz

Reputation: 103

It did end up being a CORS issue. I'm still a little iffy on exactly why, but after configuring the express/CORS package to allow requests from the IP and port of my apache server, it started working.

My understanding is that cross origin implies a different domain, where-as both of my servers are (as I understand it) on different ports on the same domain.

Either way, enabling CORS fixed the issue. Thank you to Jaromanda X for pointing it out and getting me on the right track.

Upvotes: 0

yogesh rathod
yogesh rathod

Reputation: 310

Try below code snippet.

user.save(function(err, user) {

                if(err)
                        res.send(err);

                res.status(200).json(user);

        });

Upvotes: 1

Related Questions