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

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"
  }
}

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.

It is an example use case

...but with quite unique problems

But ...

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

And            packages using them to convert html      pdf

But why web browser?

How to connect
with

  1. Separate Node.js service
     

  2. Azure Function
     

  3. NodeServices in Asp.Net Core

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 - 6.9.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

 

Its not only option to run .js code on .Net

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

Helpful links

Michał Michalczuk

michalczukm.xyz

Thank you!

michalczukm

Node.js in .Net Core ecosystem

By Michał Michalczuk

Node.js in .Net Core ecosystem

Ciklum .Net meetup, 18.09.2018 Gdańsk

  • 134
Loading comments...

More from Michał Michalczuk