Reputation: 41
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();
Upvotes: 0
Views: 296
Reputation: 4764
Right now I know two ways to do this, but I'm not comfortable with either of them, so I'll leave it to your decision...
Example 1
If you have access to Apache, then we will use Proxypass and ProxyPassReverse
to mount "express" on another port and redirect all requests to that URL
my website.com.conf from apache
#path /etc/apache2/sites-available/website.com.com.conf
<VirtualHost *:80>
ServerName website.com
ProxyRequests On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8083/
ProxyPassReverse / http://localhost:8083/
<Location /test_api>
AuthType None
Require all granted
Proxypass http://localhost:8084/test_api
ProxyPassReverse http://localhost:8083/test_api
</Location>
</VirtualHost>
My Project node media server
const NodeMediaServer = require("node-media-server");
const Express = require("express");
const app = Express();
app.get("/test_api", (req, res) => {
res.send("This is another custom endpoint!");
});
app.listen(8084, () => {
console.log(`Node Media Http Server started on port: 8084`);
});
const config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60,
},
http: {
port: 8083,
mediaroot: "./media",
allow_origin: "*",
},
trans: {
ffmpeg: "/usr/bin/ffmpeg",
tasks: [
{
app: "live",
hls: true,
hlsFlags: "[hls_time=10:hls_list_size=0:hls_flags=delete_segments]",
hlsKeep: false,
mp4: true,
mp4Flags: "[movflags=frag_keyframe+empty_moov]",
},
],
},
};
var nms = new NodeMediaServer(config);
nms.run();
As you can see, my node media server is running on port 8083 and my express server is running on port 8084, but they bot point to the same URL, website.com
Example 2
In this example, we are going to edit node media server code.
Note: I still don't agree with this example, but I've always said, you have to do what you have to do to make things work the way you want...
My list of files in my project
node-media-server (download and put in the same folder, not installing using npm, only download and put in your folder project, remember go to this folder and do npm install, to install all dependences...)
index.js
In node media server edit this file node_http_server.js
next to let app = H2EBridge(Express);
add this.app = app;
as this example
// rest of node_http_server.js file ...
this.config = config;
let app = H2EBridge(Express);
this.app = app; // add this line...
app.use(bodyParser.json());
// rest of node_http_server.js file ...
And in my index project example:
const NodeMediaServer = require("node-media-server");
const config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60,
},
http: {
port: 8083,
mediaroot: "./media",
allow_origin: "*",
},
trans: {
ffmpeg: "/usr/bin/ffmpeg",
tasks: [
{
app: "live",
hls: true,
hlsFlags: "[hls_time=10:hls_list_size=0:hls_flags=delete_segments]",
hlsKeep: false,
mp4: true,
mp4Flags: "[movflags=frag_keyframe+empty_moov]",
},
],
},
};
var nms = new NodeMediaServer(config);
nms.run();
// Add alfer npm.run()
nms.nhs.app.get("/test_api", (req, res) => {
res.send(`Hello World! ${Date.now()}`);
});
Upvotes: 0
Reputation: 839
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
});
});
Upvotes: 0