NodeJS is an open-source server environment. When installed NodeJS makes it possible to run JavaScript code outside a web browser. NodeJS is frequently used as the back-end of a webapp. In which complicated tasks can be executed. A popular framework to use NodeJS as the back-end of a webapp is Express.js. And in this article we will show how you can call Matlab from within a NodeJS express webserver.

For this example you need to have NodeJS installed on your computer.

Step 1: Create a directory for your application, and make that your current directory.

> mkdir node-matlab-app
> cd node-matlab-app

Step 2: Install the project dependencies

As mentioned this project uses the express.js framework which can be installed by using npm (Node Package Manager)
Use the npm init command to create a package.json file for your application.

> npm init
> npm install express node-fetch --save

The npm init command prompts you for a number of things, such as the name and version of your application. Just accept the default values, they can be altered later on in the package.json file if needed.

The npm install express node-fetch --save command installs Express and node-fetch in the node-matlab-app directory and saves the dependecies in the package.json file.

A small change is needed in the package.json file to be able to use the node-fetch library. Add the line

"type": "module",

to the package.json file, which should look like this now:

{
  "name": "matlab-node-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
	"start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  }
}

Step 4: Implement the Express server and run it.

Create a file index.js with the following content:

import fetch from 'node-fetch';
import express from 'express';

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

app.get('/', async(req, res) => {
  const matlabresult = await fetch('http://localhost:8081');
  const matlabdata = await matlabresult.json();
  res.send(matlabdata);   
})

app.listen(port, () => {
  console.log(`Matlab-node-app listening on port ${port}`)
})

Start the express server with the command

> node index.js

The following line should then appear on the console:

> Matlab-node-app listening on port 3000

Step 5: Install the Matlab EmbeddedServer toolbox

Make sure the examples directory is added to the Matlab path. This is needed because we will use the JSONCallback function for our example.

Step 6: Make a simple Matlab server and start it

Use the JSONCallback function that comes with the EmbeddedServer toolbox. This function returns an info structure in JSON format.
Open Matlab and type the following line in the Matlab console:

server = modelit.web.server.Server('localhost', 8081, @JSONCallback).start() 

Step 7: Open a browser at localhost:3000 

The browser calls the Express server which listens to port 3000 on localhost.
We specified that if the root '/' is called that we make an HTTP request to localhost:8081 on which the Matlab EmbeddedServer is running. Which in turn produces a .json which is returned to the Express server which in turn returns it to the browser.

The following (similar) text should be visible in the browser

{"date":"30-Nov-2022 16:30:40","method":"GET","from":"/127.0.0.1:53145"}

We made use of a simple Matlab function to generate content for our web app. To make more useful applications you can make use of .json file for information exchange with Matlab. Matlab provides json tools, to convert json to Matlab structs and vice versa. This makes it possible to make complex computations in Matlab and use the results in a web-app.

 

 

 

...