最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling an internal API from within Express - Stack Overflow

programmeradmin1浏览0评论

I have a simple API route set up in Express (consumed mainly via my Angular frontend):

 app.post('/api/sendEmail', emailApi.sendEmail);

I've made a module that sits in my backend and also needs to call this service. I figured the easiest way was to do a POST request:

  request({
    url: '/api/sendEmail',
    method: 'POST',
    json: {
      template: template.toLowerCase(),
      obj: mailObj
    }
  }, function(error, response, body){
    console.log('error', error);
    console.log('response', response);
    console.log('body', body);
  });

However, I get this error:

 Error: Invalid URI "/api/sendEmail"

What am I doing wrong here?

I have a simple API route set up in Express (consumed mainly via my Angular frontend):

 app.post('/api/sendEmail', emailApi.sendEmail);

I've made a module that sits in my backend and also needs to call this service. I figured the easiest way was to do a POST request:

  request({
    url: '/api/sendEmail',
    method: 'POST',
    json: {
      template: template.toLowerCase(),
      obj: mailObj
    }
  }, function(error, response, body){
    console.log('error', error);
    console.log('response', response);
    console.log('body', body);
  });

However, I get this error:

 Error: Invalid URI "/api/sendEmail"

What am I doing wrong here?

Share Improve this question asked Jan 6, 2017 at 6:10 JVGJVG 21.2k48 gold badges140 silver badges215 bronze badges 2
  • hostname is required in the url. And, btw, your module that tries to call the /api/sendEmail resides in different node.js application ? – Mukesh Sharma Commented Jan 6, 2017 at 6:15
  • Does this answer your question? Calling Express Route internally from inside NodeJS – abernier Commented Dec 29, 2019 at 11:57
Add a comment  | 

4 Answers 4

Reset to default 5

emailApi.sendEmail is just a function. You are much better off calling it directly. Using the network in this manner would be a serious waste of resources.

On a practical note, there are some complex issues about how to address yourself on the network. Usually you can accomplish this via localhost, but there's no guarantee that the server is listening at that address. So you'll have to take that into account.

Change Url to'http://127.0.0.1:3000/api/sendEmail', because you're calling an internal api with in express or you can also use localhost in place of 127.0.0.1.

request({
    url: 'http://127.0.0.1:3000/api/sendEmail', //on 3000 put your port no.
    method: 'POST',
    json: {
        template: template.toLowerCase(),
        obj: mailObj
    }
}, function (error, response, body) {
    console.log({error: error, response: response, body: body});
});

You need to use an absolute URI (including the protocol, domain, and port if it's not listening on the default port).

For example, if you know that the server will be listening at localhost:3000, you would want to replace your url value with 'http://localhost:3000/api/sendEmail'.

Assuming you are not using a web server like nginx and are developing on localhost. The express app does not know from where the request has originated. Try setting your Url as http://localhost:300/api/sendEmail.

发布评论

评论列表(0)

  1. 暂无评论