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: