in
ecosystem

 

How to connect them and when it might be useful?

Michał Michalczuk

michalczukm.xyz

Michał Michalczuk

Senior JavaScript Developer

Ciklum / Stibo Systems

 

 

IT trainer

infoShare Academy

       in
ecosystem

 

How to connect them and when it might be useful?

Michał Michalczuk

michalczukm.xyz

Why we'd like to use
over        ?

Performance ?

Performance ?

Easy setup?

const express = require('express');
const app = express();
const port = 3000;

app.get('/api/greeter', (req, res) => res.send({
    message: `Hello ${req.query.name}!`
}));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
{
  "name": "expressjs-example",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.3"
  }
}

Easy setup?

const express = require('express');
const app = express();
const port = 3000;

app.get('/api/greeter', (req, res) => res.send({
    message: `Hello ${req.query.name}!`
}));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
{
  "name": "expressjs-example",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.3"
  }
}

Marian

The case

In

I almost forgot - we don't have the money for license.

 

Just find some opensource.

In

I almost forgot - we don't have the money for license.

 

Just find some opensource.

What we can use in JS world?

Options in Node.js

Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium

PhantomJS is a headless web browser scriptable with JavaScript.

PhantomJS development is suspended until further notice

But why web browser?

How to connect
with

(just - how to solve our problem)

  1. Separate Node.js service
     

  2. Azure Function
     

  3. NodeServices in Asp.Net Core

It is an example use case

...but with quite unique problems

HtmlToPdf

Quotes

Node Services / JavaScript Services

nuget package: Microsoft.AspNetCore.NodeServices

Requirements

  • Node.js
    • Note: If you're deploying to an Azure web site, you don't need to do anything here - Node is already installed and available in the server environments
    • Current Azure node version - 10.14.x
  • .NET
    • For .NET Core (e.g., ASP.NET Core apps), you need at least 1.0 RC2
    • For .NET Framework, you need at least version 4.5.1.

What they can do?

Webpack dev middleware

HMR

SSR
Server Side Pre-rendering

 

Run any Node.js code

module.exports = function(callback) { 
  // In this trivial example, we don't need to receive any 
  // parameters - we just send back a string 
  
  var message = 'Hello from Node at ' + new Date().toString(); 
  callback(/* error */ null, message); 

};

NodeScripts/my-script.js

 

public async Task<string> About(
    [FromServices] INodeServices nodeServices) 
{ 
  return await nodeServices.InvokeAsync<string>(
        "NodeScripts/my-script.js"
  ); 
}

Controllers/MyController.cs

 

NodeServices has a pluggable hosting/transport mechanism, because it is an abstraction over various possible ways to invoke Node.js from .NET.

...all the built-in hosting mechanisms work by launching Node as a separate process on the same machine as your .NET code.

Other options to run
on

Edge is a bridge

using EdgeJs;

public static async void Start() 
{
    var func = Edge.Func(@"
        return function (data, cb) {
            cb(null, 'Node.js ' + 
                  process.version + 
                  ' welcomes ' + 
                  data);
        }
    ");

    Console.WriteLine(await func(".NET"));
}
var edge = require('edge');
 
var helloWorld = edge.func(`
    async (input) => { 
        return ".NET Welcomes " + input.ToString(); 
    }
`);
 
helloWorld('JavaScript', function (error, result) {
    if (error) throw error;
    console.log(result);
});

Sum it up

Its worth to run JS code in .Net ... if

  • You already wrote it
  • You want to use some unique packages

What to use?

  • Azure Function - looks nice, remember about cold start
  • Asp.Net JavaScriptServices/NodeServices - easy and nice
  • Edge.js - if you need both ways bridge 

Remember about                        sandbox

Other solutions for HTML -> PDF

Helpful links

Feedback*

 

 

*thing which speakers need

Michał Michalczuk

michalczukm.xyz

Thank you!

michalczukm

Made with Slides.com