I having a problem in sending json data from front end angularjs to express nodejs. Here is what i have tried.
frontend.html page
<form ng-submit="func()">
<textarea name="inputtext" type="text" ng-model="sentence"></textarea>
</form>
backend.js page
$scope.func = function(){
$scope.jsondata = {"status":"OK","language":"english","sentences":[{"sentence":"That's a nice restaurant."},{"sentence":"Also I went to another bad restaurant."},{"sentence":"I didn't like that movie."}]}
$http.post('/sample',$scope.jsondata).success(function(data,status){
console.log("Success");
})
}
server.js
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var bodyParser = require('body-parser');
var path = require('path');
var fs = require('fs');
app.set('views', __dirname + '/views');
app.set('view engine' , 'ejs');
app.use(bodyParser.json());
var urlencodedParser = bodyParser.urlencoded({ extended: true });
app.use(express.static('public'));
app.get('/',function(req,res){
res.render('index',{ title: 'Sentence' });
});
app.post('/sample',urlencodedParser,function(req,res){
console.log(req.body);
});
http.listen(8888, function(){
console.log("Server listening on 8888");
});
I am not getting exact JSON in the node server part. This is what i am getting.
output
{ '{"status":"OK","language":"english","sentences":': { '{"sentence":"That\'s a nice restaurant."},{"sentence":"Also I went to another bad restaurant."},{"sentence":"I didn\'t like that movie."},{"sentence":"Thats a very bad movie."}': '' } }
Can any one help, how can i get exact json in the node server part. So that i can parse and write only sentence field into a file.