Calling Node.js from ASP.NET Core

This post has been republished via RSS; it originally appeared at: Azure App Service articles.

First published on MSDN on Oct 23, 2017

Here are the steps to invoke Node.js from ASP.NET Core Website

  1. Add below line in the ConfiguraServices() in Startup.cs

    public
    void
    ConfigureServices(IServiceCollection services)


    {


    services.AddMvc();


    services.AddNodeServices();


    }




  2. Add constructor in the Controller

    public
    class
    HomeController : Controller


    {


    INodeServices
    nodeServices;


    public
    HomeController(INodeServices nodeServices)


    {


    this.nodeServices =
    nodeServices;


    }



  3. Call NodeServices as shown below

    public
    async  Task<IActionResult>
    About()


    {


    var
    msg = await
    nodeServices.InvokeAsync<string>("./hello.js",
    1);



    ViewData["Message"] = msg;


    return View();


    }



  4. Add following code to js file

    module.exports = function (callback)


    {


    var
    message = 'Hello
    world';


    callback(null, message);


    };




REMEMBER: these articles are REPUBLISHED. Your best bet to get a reply is to follow the link at the top of the post to the ORIGINAL post! BUT you're more than welcome to start discussions here:

This site uses Akismet to reduce spam. Learn how your comment data is processed.