locked
Node JS Service with ASP.Net Core RRS feed

  • Question

  • User1047888520 posted

    I have setup with ASP.Net Core with Node JS service and I want to run this. Still I get the error.

    var https = require('https'),                  // Module for https
        fs =    require('fs');                     // Required to read certs and keys
    
        var options = {
           hostname: 'api.ssg-wsg.sg',			// Production Base URL
           path: '/skillsFramework/sectors',		// This API is to retrieve all Sectors
           method: 'GET',					// Method : Get or POST
           key: fs.readFileSync('C://Users/Kishan/key.pem'),		//Input the directory for key.pem
           cert: fs.readFileSync('C://Users/Kishan/cert.pem') 		//Input the directory for cert.pem
           //passphrase: 'InputPassWord' 						//Input the passphrase, please remember to put ',' End of Line for cert 		
        };
    
        makeAPICall = function(response) {
           var str = '';    
           response.on('data', function (chunk) {
              str += chunk;
           });
    
           response.on('end', function () {
              console.log(str);
           });
        }
    
    https.request(options, makeAPICall).end();

    Please, someone help me. I really stuck in this.

    Kindly,

    Kishan

    Wednesday, November 25, 2020 2:41 PM

All replies

  • User-474980206 posted

    you don't say how you are trying to run this script.

    I assume you are trying to use Microsoft.AspNetCore.NodeServices. the issue you are having is the service runs npm, not node. npm is a package manager. it is used to build node programs (the watcher feature typically uses express to host a running version of the program).  the service is running the equivalent of:

      npm run <your script name>

    first get this to manually work, then use the node service.  if you want npm to run your program, you first need to create a package.json

       npm --init

    then add your script program to the scripts section and have node run it

       "scripts": {
          "prod": "node test.js"
         },

    then its just:

        npm run prod

    note: services source:

    https://github.com/dotnet/aspnetcore/blob/db3d23b3af6c2ade6635faac35cbaff4a94694c1/src/Middleware/SpaServices.Extensions/src/Npm/NodeScriptRunner.cs

    this is a preset round about way to start a node program. I'd probably run node directly at startup rather than using the node service. as suggested in the source, if run on windows, use cmd /c to get proper stdin/stdout redirection.

    note2: back in 2.1 days, spa services ran node directly and could execute script fragments, but this feature was dropped.

    https://github.com/dotnet/aspnetcore/issues/12890

    Wednesday, November 25, 2020 4:34 PM
  • User1047888520 posted

    Thank you for your support. I am slowly getting it. I hope I can run my scripts.

    Here, how I run the script =>

            [HttpGet]
            public async Task<IActionResult> MTLS()
            {
                try
                {
                    object postResult = await _nodeServices.InvokeAsync<string>("Scripts/CallMTLSAPI.js");
                    return Ok(postResult);
                }
                catch (Exception ex)
                {
                    return BadRequest();
                }
    
                var data = await _nodeServices.InvokeAsync<byte[]>("Scripts/CallMTLSAPI.js");
                return Ok(data);
            }

    Wednesday, November 25, 2020 5:01 PM
  • User-474980206 posted

    you need to specify the version of .net core you are using and the code you used to create _nodeServices.

    it appears you are using asp.net core 2.1 NodeServices (which is obsolete and not supported in future versions). that service requires the script be a module. simple echo module:

    module.exports = function (callback, value) {
        callback(/* error */ null, value);
    };

    to convert your script:

    var https = require('https'),                  // Module for https  
    fs = require('fs'); // Required to read certs and keys var options = { hostname: 'api.ssg-wsg.sg', // Production Base URL path: '/skillsFramework/sectors', // This API is to retrieve all Sectors method: 'GET', // Method : Get or POST key: fs.readFileSync('C://Users/Kishan/key.pem'), //Input the directory for key.pem cert: fs.readFileSync('C://Users/Kishan/cert.pem') //Input the directory for cert.pem //passphrase: 'InputPassWord' //Input the passphrase, please remember to put ',' End of Line for cert }; module.exports = function(callback) { var makeAPICall = function(response) { var str = ''; response.on('data', function (chunk) { str += chunk; }); response.on('end', function () { callback(null, str); //send back }); }; https.request(options, makeAPICall).end(); };

    its not clear you you would not just write this in c#

    Wednesday, November 25, 2020 7:37 PM