node.js - Http request from Node (Express) to Yii2 api endpoint -


i have request node yii2 api. it doesn't throw errors, doesn't return either. when request yii2 api method directly in browser, value returned. here request in route in node:

router.get('', function (req, res) {      var parameter = 20;      request({         url: 'http://**.**.**.***:8000/web/index.php?r=api/get-value',         parameter: parameter,         method: 'get'     }, function(error, response, body) {         if(error || response.statuscode != 200)             throw error;         res.send(body);     }); });  module.exports = router; 

and here method/endpoint in yii2 controllers/apicontroller.php:

public function actiongetvalue($inverterid) {     return $inverterid * 2; } 

any suggestions wrong/missing?

you can use following

var http = require('http'); var client = http.createclient(8000, 'localhost'); var request = client.request('get', '/web/index.php?r=api/get-value'); request.write("stuff"); request.end(); request.on("response", function (response) {     // handle response }); 

resource link:

  1. http request node?
  2. sending http request in node.js

or full example:

get requests

now we’ll set super simple test make sure it’s working. if it’s not still running, run simple node server it’s listening on http://localhost:8000. in separate file in same directory http-request.js new module lives, add file called test-http.js following contents:

// test-http.js  'use strict';  const     request = require('./http-request'),     config = {         method: 'get',         hostname: 'localhost',         path: '/',         port: 8000     };  request(config).then(res => {     console.log('success');     console.log(res); }, err => {     console.log('error');     console.log(err); }); 

this import our module, run request according configured options, , console log either response, or error if 1 thrown. can run file navigating directory in command line, , typing following:

$ node test-http.js

you should see following response:

success { data: 'cake, , grief counseling, available @ conclusion of test.' } 

resource link:

https://webcake.co/sending-http-requests-from-a-node-application/


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -