First you should learn how is the flow of the http requests.
This is an example using $resource an Angular built-in tool.
The following is a representation of a search functionality where I am sending as a param what the user is looking for, from a search text box in the view:
// :search is the param
angular.module("MyApp")
.factory("Search", function($resource) {
return $resource("/api/search/:search", {}, {
query : {
method : "GET",
isArray : true
}
});
});
This is the controller:
All this controllers does is to watch if there is an user typing in the input text for search and taking the what the user is writing and sending it to the backend, working along with the factory/service which is above. This functionality sends the data to the backend in order to GET a query which is an array of the result of the searched.
angular.module('MyApp')
.controller('AddCtrl', function($scope, $alert, Show, Search) {
$scope.showName = '';
$scope.data = {};
$scope.addShowToModel = '';
$scope.$watch('showName', function (tmpStr) {
if (!tmpStr || tmpStr.length == 0) return 0;
if (tmpStr === $scope.showName) {
Search.query({ 'search': $scope.showName })
.$promise.then(function(data) {
$scope.responseData = data;
})
.catch(function(response) {
console.log(response.error);
});
}
});
});
And here the code from Nodejs:
app.get('/api/search/:search', function(req, res, next) {
var searchParam = req.params.search
.toLowerCase()
.replace(/ /g, '_')
.replace(/[^\w-]+/g, '');
var parser = xml2js.Parser(); // npm module to convert xml to json
request.get('http://thetvdb.com/api/GetSeries.php?seriesname=' + searchParam, function (error, response, body) {
if (error) return next(error);
parser.parseString(body, function (err, result) {
if (result.Data.Series !== undefined) {
// this is how you send the data to the frontend
return res.status(200).send(result.Data.Series);
} else {
res.status(411).send({message: searchParam + " wasn't found"});
}
});
});
});
So, to put it simpler:
app.post('/', function(req, res){
var body = req.body;
console.log(body);
return res.send(//whatever you need to send);
};
Sometimes you don't want to send data to the frontend but a status code to see how the operation went:
app.post('/', function(req, res){
if(everythingIsFine) {
// send only status code
res.sendStatus(200);
}else {
// in case you want to send status + message
res.status(401).send('Invalid whatever thing');
}
};
Hope it helps!
EDIT
In the service, you can use $http instead of $resource. That is the not the important thing on my answer but just telling you. According to a comment:
It would be more appropriate to use $http.get instead of $resource. $resource is intended for RESTful CRUD endpoints. A search endpoint does not fit that specification.