0

I have a NodeMediaServer to livestream to and save the vods. I want to add a custom endpoint to the HTTP server of NodeMediaServer. Is this possible or do I have to run an express server next to it? I know it is possible to access a single file but i havn't found a way to get all files which is what i want to accomplish.

const NodeMediaServer = require('node-media-server');
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');

const express = require('express');

const app = express();

app.get('/vods', (req, res) => {
  res.send('Hello, this is a custom endpoint!');
});

const config = {
  logType: 3,
  http_api: 1,

  rtmp: {
    port: 1935,
    chunk_size: 60000,
    gop_cache: true,
    ping: 30,
    ping_timeout: 60
  },
  http: {
    port: 8000,
    allow_origin: '*',
    mediaroot: 'vods'
  }
};

var nms = new NodeMediaServer(config, app)

nms.run();

1 Answers1

0

to add routes in node-media-server with your current implementation you can do something like

const app = express();

app.get('/vods', (req, res) => {
  res.send('Hello, this is a custom endpoint!');
});

app.get('/custom', (req, res) => {
  res.send('This is another custom endpoint!');
});

...rest of your code

To get all media you can do something like:

app.get('/all-vods', (req, res) => {
  const mediaPath = path.join(__dirname, 'vods'); // Path to the media directory
  fs.readdir(mediaPath, (err, files) => {
    if (err) {
      console.error(err);
      return res.status(500).send('Internal Server Error');
    }
    res.json(files); // Send the list of media files as JSON response
  });
});
ardritkrasniqi
  • 739
  • 5
  • 13