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

人盡茶涼 提交于 2019-12-11 05:29:39

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/12113999/how-can-i-view-a-list-of-my-files-in-a-directory-on-sinatra

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!