1

I am using Angular for the front-end and attempting to write a JSON object to a file called 'post.json' in the same directory as index.html. I can make it work using PHP but I want to know how do it with Node.js. I have looked at a lot of posts online but maybe I am not understanding how http POST actually works and what settings the server needs to write to file from an Angular app. How do I write to file from the Angular app and what settings does the node server need?

Code in the Angular file:

// Add a Item to the list
$scope.addItem = function () {

    $scope.items.push({
        amount: $scope.itemAmount,
        name: $scope.itemName
    });

    var data = JSON.stringify($scope.items);

    $http({
        url: 'post.json',
        method: "POST",
        data: data,
        header: 'Content-Type: application/json'
    })
    .then(function(response) {
        console.log(response);
    }, 
    function(response) {
        console.log(response);
    });

    // Clear input fields after push
    $scope.itemAmount = "";
    $scope.itemName = "";
};

This is the node server file:

var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(8080);

fs = require('fs');
fs.open('post.json', 'w', function(err, fd){
    if(err){
        return console.error(err);
    }
    console.log("successful write");
});

I am then getting this error:

enter image description here

2
  • Did you set up the server itself? Set up routes, that is? Commented Feb 19, 2016 at 0:22
  • Sounds like that is what I need. I'll look into it, thanks. Commented Feb 19, 2016 at 0:43

1 Answer 1

1

Here is example of Node.js server using Express.js framework (in case you are not limited to 'connect').

var express = require('express');
var app = express();
var fs = require('fs');

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.post('/', function (req, res) {
  fs.writeFile(__dirname+"/post.json", req.body, function(err) {
    if(err) {
       return console.log(err);
    }
    res.send('The file was saved!');
  }); 
});

app.listen(8080, function () {
  console.log('Example app listening on port 8080!');
});

In your angular controller specify explicitly the url:

 $http({
    url: 'http://localhost:8080',
    method: "POST",
    data: data,
    header: 'Content-Type: application/json'
})

EDIT:

Removed body-parser middleware for simplification.

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

1 Comment

This works, I just added res.sendFile(__dirname + '/index.html'); instead of res.send('Hello World!'); and then created a folder named static which contains all my static files then added this app.use(express.static('static')); to node.js server file. Thanks. Looks clean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.