How to Use Machine Learning In Express.js/Node.js with Brain.js
In this tutorial we will learn how to embed machine learning into an express web application. There are several methods of embedding ML into an app. Usually the best method is to train the model and used the saved model in the web app.
In this tutorial we will only see the other alternative.
The requirements includes the following:
- Node.js (npm)
- Brain.js( Any ML package such as Tensorflow.js)
- Express.js
- Body-parser
- Ejs
We will create a folder and initialize the folder with npm init to build a package.json file to track all our necessary packages we will be using.
Then add the following packages
npm install express body-parser brainjs ejs –save
To begin, let us work on the front-end and then the back-end.The most important thing is to know how to parse our data from our front-end to our back-end into our machine learning part.
We can use the body-parser to enable us achieve this.
The Intended Structure of the app
- app.js
- view (folder)
- — index.ejs
- — results.ejs
- data.json
Front-End
We will then create two files with the extension .ejs which is the type of template engine we will be using inside a folder named as view.This is where will place our files.
We will be using materialize.css for the beautification.
One the index.ejs file we will place the following code.
<form method="POST" action="/predict">
<input type=”submit” value=”Predict” class=”btn btn-small purple waves-effect waves-light btn-extend”> <input type=”reset” value=”Clear” class=”btn btn-small white waves-effect waves-light btn-extend”> </form>
The most important things here in the form is the method which is a POST method and the action(action=”/predict) which is for our route where the data will be sent to.We will create that latter.
We will also give the input type the name (message) which will be what will be used to identify and store the typed input to be used in the back-end.
Place the following code in the results.ejs
Results of Prediction
Your Message was:
We will use ejs template <%= resultdata %> to display our results from the back-end.
Back-End
We will create an app.js and place the following code there
var express = require('express');
var bodyParser = require('body-parser');
//ML Aspects with Brainjs const brain = require('brain.js'); const data = require('./data.json'); const network = new brain.recurrent.LSTM(); const trainingData = data.map(item => ({ input: item.text, output: item.category }));
network.train(trainingData, { iterations: 2000 }); // Express App Initialized var app = express(); var urlencodedParser = bodyParser.urlencoded({ extended: false }) app.set('view engine','ejs'); app.get('/',function(req,res){ // res.send('Hello world this is Express'); res.render('index'); }); app.get('/predict',function(req,res){ console.log(req.query); res.render('index'); }) app.post('/predict',urlencodedParser,function(req,res){ console.log(req.body); const output = network.run(req.body.message); console.log(`Category: ${output}`); res.render('results',{mydata:req.body.message,resultdata:output}); }) app.listen(4000,function(){ console.log("Listening on localhost:4000"); });
We will be using bodyparser to parse our data from the front-end to our backend.
var urlencodedParser = bodyParser.urlencoded({ extended: false })
and then use the urlencodedParser varialble in our predict route as shown below
app.post(‘/predict’,urlencodedParser,function(req,res){
console.log(req.body);
…});
The data then will be parsed into our machine learning part via the
const output = network.run(req.body.message);
and render the result to our result.ejs file on our front-end.
Full Code on Github
Below is a video tutorial
Thanks For Reading
Jesus Saves
Gleb and Vlad… Hats off to your clarity, simplicity and honesty with which you have written this article. Very Informative and balanced. Keep on churning more.
I appreciate this Sharma, Glad it was informative