简体   繁体   中英

Node.js list files under directory using Express

On server side:

var express = require('express');
var router = express.Router();
var fs = require('fs');

var images = fs.readdirSync('./images/');
var videos = fs.readdirSync('./videos/');

router.get('/', function(req, res) {
  res.render('index', {
    images: images,  // I want to pass this list of images to jade file
    videos: videos
  });
});

module.exports = router;

index.jade:

extends layout

block content
  h1= images

And it renders nothing. Apparently, the images parameter hasn't been passed to the jade file. How can I get somthing like:

<img src="1.png" />
<img src="2.png" />

Your code looks correct, but if you aren't seeing any output this means your images variable is a fasly value.

Make sure your './images/' folder exists, and is accessible to your node process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM