How can I view a list of my files in a directory on sinatra?

I run an project on Sinatra, now it has a lot of files a sub-directories, so I want to view a list of those files with their respective directory. For example, I put in the address bar "localhost:4567/landing/" where landing is the container of some pages, but it throw me this "Sinatra doesn’t know this ditty." Is there a way to do that. I hope you understand what I'm asking.

Answers


Sinatra doesn't deal with the local file system, it deals with HTTP routes. For example, the '/' in get '/' do refers to the root url of your website, not the root directory of your website. In order to list the local files, you would need to use the Ruby Dir class, something like this:

#!/usr/bin/env ruby

require 'rubygems'
require 'sinatra'

get '/' do
   Dir.entries('.').map { |e| "<p>#{e}</p>" }
end

Edit: Of course, it's dangerous to work with the filesystem directly, even using routes, so I would recommend that you get a better understanding of what is going on behind the scenes by reading up a little.


Need Your Help

How does rule-based verification with Petri nets work and what is this good for?

rules petri-net

A friend just pointed me out to an new development in computer science called "rule based verification with Petri nets". He's not a computer guy but an artist, so I wonder if that's really somethin...

Wix - Custom Action Return Code - Handling

c# wix custom-action

Please read carefully as I will explain what I'm trying to do. I have a wix installer that calls custom action to validate user input. I want to call next dialog if validation is successful and NOT...