Skip to content
The Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written) OpenAssistant Conversations (oasst2)

write me a program in nodejs with express and ejs. i want it to recursively scan a…

In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
  1. User

    write me a program in nodejs with express and ejs. i want it to recursively scan a directory, and serve a nested list of files and folders as a webpage

  2. Assistant

    Awesome! Let's get started on this exciting project!

    First, we need to set up our project by creating a package.json file and installing the necessary dependencies. Open your terminal and navigate to the directory where you want to create your project folder.

    1. Run npm init and follow the prompts to create your package.json file.
    2. Install the dependencies by running npm install express ejs.

    Now that we have our project set up, let's create our main file app.js.

    1. Require the necessary modules as follows:
    const express = require('express');
    const ejs = require('ejs');
    const path = require('path');
    const fs = require('fs');
    
    1. Create an instance of the express module as follows:
    const app = express();
    
    1. Set up the view engine to use ejs as follows:
    app.set('view engine', 'ejs');
    
    1. Create a middleware function to serve static files from the public folder as follows:
    app.use(express.static(path.join(__dirname, 'public')));
    
    1. Create a route for the homepage as follows:
    app.get('/', (req, res) => {
      const directoryPath = path.join(__dirname, 'public');
      const root = {
        name: 'public',
        children: []
      };
      createTree(directoryPath, root);
      res.render('index', { root });
    });
    
    1. Create a function to recursively scan the directory and create the nested file and folder structure as follows:
    function createTree(directoryPath, node) {
      const files = fs.readdirSync(directoryPath);
    
      for (const file of files) {
        const filePath = path.join(directoryPath, file);
        const stats = fs.statSync(filePath);
    
        if (stats.isDirectory()) {
          const directory = {
            name: file,
            children: []
          };
          node.children.push(directory);
          createTree(filePath, directory);
        } else {
          node.children.push({ name: file });
        }
      }
    }
    
    1. Create an index.ejs file in the views folder and render the nested file and folder structure as an unordered list as follows:
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>File Explorer</title>
    </head>
    <body>
      <h1>File Explorer</h1>
      <ul>
        <% for (const child of root.children) { %>
          <li>
            <%= child.name %>
            <% if (child.children) { %>
              <ul>
                <% for (const grandChild of child.children) { %>
                  <li>
                    <%= grandChild.name %>
                    <% if (grandChild.children) { %>
                      <ul>
                        <% for (const greatGrandChild of grandChild.children) { %>
                          <li>
                            <%= greatGrandChild.name %>
                            <% if (greatGrandChild.children) { %>
                              <!-- add more nested ul elements as needed -->
                            <% } %>
                          </li>
                        <% } %>
                      </ul>
                    <% } %>
                  </li>
                <% } %>
              </ul>
            <% } %>
          </li>
        <% } %>
      </ul>
    </body>
    </html>
    

    That's it! You now have a program in Node.js with Express and EJS that recursively scans a directory and serves a nested list of files and folders as a webpage.

    To run the program, simply run node app.js in your terminal and navigate to http://localhost:3000 in your browser.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.