0

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.

3
  • As it sounds like you're just looking to mock a back end with a Node solution and a JSON flat file, you may want to check out json-server, which does just that. Commented Jul 27, 2015 at 12:01
  • Actually I'm just learning, I don't know if I'm going to be full-stack dev, but for now I just want to understand simple back-end manipulations with NodeJS. So I just need to start my App on a simple server and make post request. I mean I want to understand how does it works. I've seen a lot of questions like my but no one clear answer. So I guess many people would be happy to know this :) Commented Jul 27, 2015 at 12:14
  • Well, you can extend json-server as a module in a Node/Express app too. Either way, it's handy for working on a front-end without worrying about the back-end. Commented Jul 27, 2015 at 16:38

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot beauty for your help, I'll try :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.