How to send client form to node/express server?

I’m using node.js and express to run train an ai model and make predictions on new data.

I’m using ml.js for the model.

const express = require('express')
const rf =  require('ml-random-forest');

const options = {
    seed: 3,
    maxFeatures: 0.9, //number of features
    replacement: true,
    nEstimators: 25 //number of trees

  var RF = new rf.RandomForestClassifier(options);


const predictedLabel = [[45, 29, "3:09:00", "01/04/2020"]]; //new data predict

let result = RF.predict(predictedLabel); //predict the new data
const app = express()
const port = 3000

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

app.get('/', (req, res, next) => {

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))

What I would like to do next is allow for the user to input in a form new data which would send to the predictedLabel and output the result back to the user.

The form would be something like:


which would send the data to the server.

I’m not sure exactly how to implement the client side, maybe a web socket or something? Any ideas would be great!
Thanks so much!!

If you want to send data from the page to the server, then use either fetch or sockets would be fine (socketio.js for example).

If I get your idea, this would be the basic ingredients:

  1. Write out the form, inside <form>
  2. On the submit event gather each input like so:
form.addEventListener("submit",  (e)=>{
//gather user input values
//fetch etc
  1. Include that information on a JSON object and send it to server using fetch
fetch(url, {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *cors, same-origin
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *same-origin, omit
    headers: {
      'Content-Type': 'application/json'
    redirect: 'follow', // manual, *follow, error
    referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
    body: JSON.stringify(data) // body data type must match "Content-Type" header

MDN Fetch

I’m also onto machine learning so we could team up at some point :slight_smile:

1 Like

@Minsky thanks so much for your help!

That would be great :grinning:

1 Like