Odinn
Odinn

Reputation: 1106

How to update JSON file with Angular and Node?

I'm totally new in Backend, I'm trying to get the data from form and push it to the JSON file. I've tried to find the solution, but in most situation examples or separated or with MongoDB, but for learning I would like to do it just with Angular and Node.
What I have:

  1. In Front-end I have submit function with says:
    "Failed to load resource: the server responded with a status of 405 (Method Not Allowed) http://127.0.0.1:8080/Form/json/data.json " . Of course.

        $scope.submit = function(person) {
    
            $http.post('json/data.json', $scope.data).then(function(){
                $scope.msg = 'Saved';
            });
        };
    
  2. Back-end. I'm new in Node as I've already said, that's why my server is something like this:
var http = require('http');
var express = require('express');
var server = express();
server.use(express.static(__dirname));
var PORT = 8080;
server.listen(PORT, function() {
    console.log(PORT);
})

As I understand, I need to do a Post request at first from Angular controller to my server and than from the server Post request to my Json file? Please can someone to explain steps how to do it and simple code would be great.

Upvotes: 0

Views: 1499

Answers (1)

Stacey Burns
Stacey Burns

Reputation: 1092

With your current code, Express doesn't know what function to call when it receives the request. You need to create a route for each request that will be sent to the server:

    server.post('/json/data ', function (req, res) { 

             /// Do something with the req data here, then send back res ///
   });

More information can be found here: http://expressjs.com/guide/routing.html

Upvotes: 1

Related Questions