A sharing about handling the WebClientDat's upload file filed on the server-side with sample code (nodejs express)

Recently, my project is about uploading images generated from TouchDesginer to a web server for further processing. I’m new to TouchDesginer, and I found a DAT named Web Client Dat, which allows us to assign an upload file while making a request. I read the online documentation, and I haven’t found any information about how the file will be sent to the server, like the request content type or things like that. I spent some time doing research about how to receive the file from the server-side. Eventually, I did it. So that I’d like to share with you guys how I made it. Perhaps, It might help someone to save some time in the future while dealing with WebClientDat and Upload files.

The WebClientDat fires the request by passing the given file as a chunked file. So that it doesn’t work like multipart/form-data, there won’t be any key-value concept while getting the file’s data.

For example, I use NodeJs express as the web server. I had tried to use busboy + express. The busboy is a package for handling chunked files. It doesn’t work for handling the request from WebClientDat because it expects multipart/form-data.

Eventually, I found the way. Here is a simplified version of the code I used to grab the data.

const fs = require("node:fs");
const path = require("node:path");
const express = require("express");

//Init the web server powered by express
const app = express();
app.put("/minting", (req, res) => {
  //Create folder if it hasn't been created
  const uploadFolder = `${__dirname}/uploads`;
  if (!fs.existsSync(uploadFolder)) {
    fs.mkdirSync(uploadFolder, { recursive: true });
  }

  const uploadFilename = `${Date.now()}.jpg`;
  const uploadPath = path.join(uploadFolder, uploadFilename);

  // Create Write stream
  const writeStream = fs.createWriteStream(uploadPath);
  writeStream.on("error", (err) => {
    //TODO: handle error accordingly please.
    return res.status(500).end("");
  });

  // Handle end event
  req.on("end", () => {
    return res.status(200).end(uploadPath);
  });

  // Start pipe
  req.pipe(writeStream);
});
app.listen(80);

Please feel free to let me know your thoughts. Perhaps, there’s a smarter or better way to handle it? Also, i think if the doc of WebClientDat could have more details or sample about how to handle the upload file on server side, it would be amazing.