I am making a video player app, I want to list all the files in my directory to access through the server. I am also using authentication through MongoDB. I want to list all the files in the videos.ejs page so that user can see the listing of the files in the directory and access those files. I am using node.js and EJS but I am stuck. It would be really nice if you help me with this. Here is my app.js:
//jshint esversion:6
require('dotenv').config();
const express = require ("express");
const bodyParser = require ("body-parser");
const ejs = require ("ejs");
const mongoose = require("mongoose");
const encrypt = require("mongoose-encryption");
const testFolder = 'public/vids/';
const fs = require('fs');
const app = express();
var list = "" ;
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect("mongodb://localhost:27017/userDB", {useNewUrlParser: true});
const userSchema = new mongoose.Schema({
email: String,
password: String
});
const secret = "Thisismylilkey";
userSchema.plugin(encrypt, {secret: secret, encryptedFields: ['password'] });
console.log(fs.readdirSync);
const User = new mongoose.model("User", userSchema);
app.use(express.static("public"));
app.get("/register", function(req, res){
res.render("register");
});
app.post("/register", function (req, res) {
const newUser = new User({
email: req.body.username,
password: req.body.password
});
newUser.save(function(err){
if(err){
console.log(err);
}else{
res.render("home");
}
});
});
app.get("/", function (req, res) {
res.render("login");
});
app.post("/", function (req, res) {
const username = req.body.username;
const password = req.body.password;
User.findOne({ email: username }, function (err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
if (foundUser.password === password) {
res.render("home");
app.get("/home", function (req, res) {
res.render("home");
});
app.get("/music", function (req, res) {
res.render("music");
});
app.get("/videos", function (req, res) {
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
list = file;
console.log(list);
});
});
res.render("videos", { listing: list });
});
app.get("/documents", function (req, res) {
res.render("docs");
});
app.get("/photos", function (req, res) {
res.render("photos");
});
app.get("/player", function(req, res){
res.render("player")
});
}else{
res.send("Get the hell out of here :p")
}
}
}
});
});
app.listen(3000, '0.0.0.0' ,function(){
console.log("server started at port 3000");
});
Here is my videos.ejs:
<%- include("partials/header"); -%>
<h1 class="pg-title">videos</h1>
<div>
<video width="auto" height="240" controls>
<source src="/vids/<%= listing %>" type="video/mp4">
</video>
<a href="/player"><%= listing %></a>
<h1>name <%= listing %> </h1>
</div>
<%- include("partials/footer"); -%>
In your post /
route you are routing all paths, which is WRONG way on doing, you will need to create a separate route for all separate paths.
Now answering your primary question,
there is an inbuilt fs module
in node.js using it you can get all files and folders in a specific path, check this https://nodejs.org/api/fs.html for more details,
Sample code to read files in the given path, instead of logging you can send files variable in below example to ejs to render it, (also path name I used below is of where my app.js is present viz __dirname)
let fs = require('fs');
let files = fs.readdirSync(__dirname);
console.log(files);