Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const router = express.Router();
- const fs = require('fs');
- const path = require('path');
- const root = { }
- router.get("/:filePath", (req, res, next) => {
- jsonData = root
- jsonData.folders = []
- jsonData.files = []
- var folderPath = req.params.filePath,
- folderPath = folderPath.replace("+", "/"),
- folderPath = `/home/cameron/TestRestApi/api/stoage/${folderPath}`
- function walkDir(dir) {
- fs.readdirSync(dir).forEach(f => {
- let dirPath = path.join(dir, f);
- if (fs.lstatSync(dirPath).isDirectory()) {
- const folders = {
- serverPath: dirPath,
- name: f,
- }
- jsonData.folders.push(folders)
- } else {
- var splitPath = '1/2/3/4',
- splitPath = splitPath.replace(/\//g, '+');
- const fileExt = path.extname(dirPath)
- const files = {
- serverPath: dirPath,
- name: f,
- fileType: fileExt,
- reqPath: splitPath
- }
- jsonData.files.push(files)
- }
- })
- }
- walkDir(folderPath);
- res.status(200).json(jsonData);
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement