I have this part of C# code that works fine with another NodeJS Express app to which source code I don't have access, and I would like to leave this code as is.
string filetoupload = @"D:\testvideo.mp4";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://127.0.0.1:10090" + "/upload?name=" + WebUtility.UrlEncode(Path.GetFileName(filetoupload)));
request.Method = "PUT";
request.ContentType = MimeTypesMap.GetMimeType(Path.GetExtension(filetoupload));
using (FileStream fs = File.OpenRead(filetoupload))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
request.ContentLength = buffer.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Flush();
requestStream.Close();
WebResponse upresponse = request.GetResponse();
Stream updataStream = upresponse.GetResponseStream();
StreamReader upreader = new StreamReader(updataStream);
string upres = upreader.ReadToEnd();
Console.WriteLine("RES: " + upres);
}
Problem is I don't know how to properly read stream data on NodeJS Express end. I have managed to read it and to store file on disk using this code
const express = require('express');
const fs = require('fs');
const app = express();
const port = process.env.PORT || 10090;
app.listen(port, () =>
console.log(`App is listening on port ${port}.`)
);
app.put('/upload', async (req, res) => {
try {
var socket = req.socket;
socket.on('data', function(data) {
fs.appendFile("somevideo.mp4", new Buffer(data), function (err,data) {
if (err) {
res.status(500).send(err);
}
});
});
res.send({
status: true,
message: 'File uploaded'
});
} catch (err) {
res.status(500).send(err);
}
});
But I need to do some more work after file is stored on disk (creating hash code, do some MySQL work, etc.) and then return some text data as response message. Problem for me is how to know when that has happened?
Is there another (right) way to store files uploaded like this?