Tuesday, 2 May 2017

why won't my answers .post to my data json file node.js

hello here is the website for the my attempt homework:

https://friendfinderrevisited-aromin.herokuapp.com/survey

when i am trying to just post anything to my data/friends.js file after clicking the submit button

i am receiving a:

api/friends Failed to load resource: the server responded with a status of 404 (Not Found)

Even though https://friendfinderrevisited-aromin.herokuapp.com/api/friends does exist

The first code is my api routing file and

the second is the script that runs after i click the submit button on the survey page. I have looked at solution and other classmates code about the .post after code, but it looked unneeded at this point which is why i commented it out.

// requiring friendList data
var friends = require('../data/friends.js');
var path = require("path");


// exporting the function
// what is the purpose of the apps parameter?
module.exports = function(app) {

// using this route to show a body-parser version of this json response
        app.get("/api/friends", function(req, res) {
          res.json(friends);
        });

// allowing users to post their info and compare it to users in the system already
        app.post("api/friends", function(req, res) {
    // Here we take the result of the user"s survey POST and parse it.
        var addTo = req.body;
        friends.push(addTo);
        });

}
<script>
$('#submitId').on('click', function(event) {
        event.preventDefault();
        var newFriend = {
                name: $('#nameId').val(),
                score: [
                                $('#question1').val(), 
                                $('#question2').val(), 
                                $('#question3').val(), 
                                $('#question4').val(), 
                                $('#question5').val(), 
                                $('#question6').val(), 
                                $('#question7').val(), 
                                $('#question8').val(), 
                                $('#question9').val(), 
                                $('#question10').val()
                        ]
        };     

        var currentURL = window.location.origin;

        $.post(currentURL + "/api/friends", newFriend, function(data) {
                console.log(data);
                
          // Grab the result from the AJAX post so that the best match's name and photo are displayed.
          // $("#nameId").text(data.name);
          // Show the modal with the best match
          // $("#results-modal").modal("toggle");

    });
});     
</script>


via Christian A

No comments:

Post a Comment