Skip to content

Instantly share code, notes, and snippets.

@sdras
Created April 8, 2018 03:04
Show Gist options
  • Save sdras/f5665c5bcd98b48b4a3a9aed1312fd37 to your computer and use it in GitHub Desktop.
Save sdras/f5665c5bcd98b48b4a3a9aed1312fd37 to your computer and use it in GitHub Desktop.
const http = require('http')
const hostname = '127.0.0.1'
const port = 3000
//modified from this original: https://stackoverflow.com/questions/11194287/convert-a-directory-structure-in-the-filesystem-to-json-with-node-js/11194896
function files() {
var fs = require('fs'),
path = require('path')
function dirTree(filename) {
var stats = fs.lstatSync(filename),
info = {
path: filename,
name: path.basename(filename)
}
if (stats.isDirectory()) {
info.type = 'folder'
info.children = fs.readdirSync(filename).map(function(child) {
return dirTree(filename + '/' + child)
})
} else {
info.type = 'file'
}
return info
}
var util = require('util')
var output = dirTree('vue/')
return JSON.stringify(output)
}
const server = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end(files())
})
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment