如何列出目录 node.js EJS 中的所有文件
How can I list all the files in a directory node.js EJS
我正在制作一个视频播放器应用程序,我想列出我目录中的所有文件以通过服务器访问。我还通过 MongoDB 使用身份验证。我想列出 videos.ejs 页面中的所有文件,以便用户可以查看目录中的文件列表并访问这些文件。
我正在使用 node.js 和 EJS,但我卡住了。如果你能帮我解决这个问题,那就太好了。这是我的 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");
});
这是我的 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"); -%>
在您的 post /
路由中,您正在路由所有路径,这是 WRONG 的做法,您需要为所有单独的路径。
现在回答您的主要问题,
node.js 中有一个内置的 fs module
使用它你可以获取特定路径中的所有文件和文件夹,查看此 https://nodejs.org/api/fs.html 了解更多详细信息,
读取给定路径中文件的示例代码,而不是日志记录,您可以将下面示例中的文件变量发送到 ejs 以呈现它,(我在下面使用的路径名也是我的 app.js 所在的位置即 __dirname)
let fs = require('fs');
let files = fs.readdirSync(__dirname);
console.log(files);
我正在制作一个视频播放器应用程序,我想列出我目录中的所有文件以通过服务器访问。我还通过 MongoDB 使用身份验证。我想列出 videos.ejs 页面中的所有文件,以便用户可以查看目录中的文件列表并访问这些文件。 我正在使用 node.js 和 EJS,但我卡住了。如果你能帮我解决这个问题,那就太好了。这是我的 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");
});
这是我的 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"); -%>
在您的 post /
路由中,您正在路由所有路径,这是 WRONG 的做法,您需要为所有单独的路径。
现在回答您的主要问题,
node.js 中有一个内置的 fs module
使用它你可以获取特定路径中的所有文件和文件夹,查看此 https://nodejs.org/api/fs.html 了解更多详细信息,
读取给定路径中文件的示例代码,而不是日志记录,您可以将下面示例中的文件变量发送到 ejs 以呈现它,(我在下面使用的路径名也是我的 app.js 所在的位置即 __dirname)
let fs = require('fs');
let files = fs.readdirSync(__dirname);
console.log(files);