I'm trying to send a request to my ExpressJS server using AngularJS:
angular.module('app').controller('contactCtrl', function($scope, $http) {
$scope.envoyer = function(nom, organisation, courriel, telephone, message){
$http.post('/contact', {nom:nom, organisation:organisation, courriel:courriel, telephone:telephone, message:message}).then(function(error, response){
if(response.data.success){
console.log('sent!');
}
});
}
});
This is the code from the server:
var mailer = require('nodemailer');
var EMAIL_ACCOUNT_EMAIL = '[email protected]';
var EMAIL_ACCOUNT_PASSWORD = 'aaa';
var smtpTransport = mailer.createTransport({
service: "Gmail",
auth: {
user: EMAIL_ACCOUNT_EMAIL,
pass: EMAIL_ACCOUNT_PASSWORD
}
});
app.post('/contact', function(req, res, next){
var success = true;
var mailOptions = {
to: '[email protected]',
subject: 'qsdxwccvfd',
html: 'sqdqsdq'
};
smtpTransport.sendMail(mailOptions, function(error, res){
if(error){
console.log('[ERROR] Message NOT sent: ', error);
success = false;
} else {
console.log('[INFO] Message sent: ' + res.message);
}
next(error, success);
});
});
After the server performs the request, I get this error message on the client side:
POST http://localhost:3002/contact 404 (Not Found) angular.js:8495
(anonymous function) angular.js:8495
sendReq angular.js:8291
$http.serverRequest angular.js:8025
wrappedCallback angular.js:11498
wrappedCallback angular.js:11498
(anonymous function) angular.js:11584
Scope.$eval angular.js:12608
Scope.$digest angular.js:12420
Scope.$apply angular.js:12712
(anonymous function) angular.js:18980
jQuery.event.dispatch jquery.js:4409
elemData.handle
I think that it's coming from the fourth line of the AngularJS code, but I wasn't able to find how to fix it. How can I fix that, please?
http://localhost:3002/contact. Were you expecting something to be there? What code is providing the implementation of that endpoint?/contactdoesn't return a response. Assuming that's the entire server code (and there isn't some later middleware handling it) then the call tonextwill return your 404. If you'd like an example of returning JSON data for the client to parse then let me know and I can write one up, assuming that's anexpressapp.