0

I am trying to perform a GET request from my Angular app controller when the "Send" button is pressed, as follows:

var config = {
  method: 'GET',
  url: '/sendmail',
  params: {
    from: $scope.name,
    email: $scope.email,
    message: $scope.message
  }
};

$http(config).then(function (res) {
  console.log("this message is successfully printed!");
});

In my Node.js Express app, I have:

app.get('/sendmail', function (req, res) {
  // This should print, but never does.
  console.log("Got mail!");
});

Why do you think the Express function is not picking up the call, even though the Angular app is sending it?

Joshua
  • 648
  • 7
  • 18
Grateful
  • 9,685
  • 10
  • 45
  • 77
  • Your URL is ``/send`` in the Angular script, but ``/sendmail`` in Express. – Joshua Feb 09 '17 at 07:49
  • No... sorry, I am doing it correctly in my code.. I just mistyped here. Thanks for notifying. – Grateful Feb 09 '17 at 07:50
  • Have you tried sending request to your sever outside Angular app? e.g. accessing it directly in browser? – Bartek Fryzowicz Feb 09 '17 at 07:52
  • @bartekfr yes, I tried in the browser directly.... but it still doesn't call the function... and it seems to me that my angular ui-router is interfering – Grateful Feb 09 '17 at 07:54
  • @bartekfr I just tried making the request from postman... and I was surprised to see that I am getting my entire page HTML back! That may tell us something.... – Grateful Feb 09 '17 at 08:00

1 Answers1

0

Maybe somewhere in your server code you set serving 'index.html' for all requests (normal practice for SPA)? You should make sure that your code handling GET /sendmail is placed before code which serves index.html

Bartek Fryzowicz
  • 6,464
  • 18
  • 27
  • Oh amazing! That was exactly the problem! Thank you so much man. However, can you tell me one more thing... what's the difference between `app.use` and `app.get`. Since, within my node app.js, I am using `app.use(function (req, res) { res.sendFile(path.join(__dirname, '/index.html')); });`... would it make any difference to use `app.get('/', function (req, res) { res.sendFile(path.join(__dirname, '/index.html')); });` instead? – Grateful Feb 09 '17 at 08:04
  • https://expressjs.com/en/api.html#app.use - It's for middleware, such as doing something specific for every request. – Joshua Feb 09 '17 at 08:08
  • I'm glad I could help :) regarding differences between `use` and `get` check this answer: http://stackoverflow.com/questions/15601703/difference-between-app-use-and-app-get-in-express-js – Bartek Fryzowicz Feb 09 '17 at 08:10
  • @BartekFryzowicz Yes, most certainly. :) – Grateful Feb 10 '17 at 04:14