My stream in NodeJS not working.

Options
just_relax
just_relax Member Posts: 1

Hello, I'm having trouble getting a video from Cloudinary and sending it to my front end via streams. Here's the code:



import express, { Request, Response } from "express";

import request from "request";

import { PassThrough } from "stream";

import cloudinary from "cloudinary";

import * as dotenv from "dotenv";

dotenv.config();


const app = express();


cloudinary.v2.config({

  cloud_name: process.env.CLOUD_NAME,

  api_key: process.env.API_KEY,

  api_secret: process.env.API_SECRET,

});


app.get("/video", (req: Request, res: Response) => {

  const videoId = process.env.VIDEO_ID;

  const format = "mp4";

  const cloudinaryStream = cloudinary.v2.video(videoId, {

    resource_type: "video",

    format: format,

    streaming_profile: "hd",

  });


  const passThroughStream = new PassThrough();


  res.writeHead(200, {

    "Content-Type": `video/${format}`,

    "Content-Disposition": "inline",

  });


  cloudinaryStream.on("error", (err: Error) => {

    console.log("Error:", err);

    passThroughStream.destroy(err);

  });


  passThroughStream.on("error", (err: Error) => {

    console.log("Front error:", err);

  });


  passThroughStream.on("close", () => {

    console.log("Vídeo concluído");

  });


  passThroughStream.pipe(res);

  request

    .get(cloudinaryStream)

    .pipe(passThroughStream as unknown as NodeJS.WritableStream);

});


app.listen(3000, () => {

  console.log("Running in 3000");

});

-----------------------


My error happens when using "on" in cloudinaryStream variable. Error: Property 'on' does not exist on type 'string'.

Tagged:

Answers

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 105
    Options

    Hey, thanks for getting in touch with your question.

    Using cloudinary.v2.video is actually a helper method that generates a video tag, and you can read about it here.

    If you wanted to get the exact URL so you can stream in the video's contents, you should be able to use `cloudinary.url`. For example: cloudinary.url(videoId, {resource_type: "video", format: format, streaming_profile: "hd"});

    Could you please give that a try and let us know how it goes?

    Thanks,

    -Danny