HTTP Server in 5 Lines With Webrick
2014-01-08 10:54
302 查看
Usually when I am prototyping a web UI - either in Javascript or Flex, I would just write a static html, because that's the simplest thing that works. But, once in a while, it doesn't work because of the security restrictions that the browser imposes on local
files. Maybe you want to use ajax calls(which is sometimes problematic on IE), trying to use the google maps api, or the FABridge, whatever the reason may be. Well, you can get around this problem easily using this ruby script:
This runs a web server at http://localhost:1234/ which
mounts the top level directory to your current directory.
Update: Oops, it's not exactly that easy after all. In order to prevent caching - which you will want to do if you are doing development - you will want to write an extra class. The modified script:
Not 5 lines anymore, bummer! The code for NonCachingFileHandler was stolen from unittest_js.
Add
, {:FancyIndexing => true} to the end of mount line,as Follows:
server.mount "/", NonCachingFileHandler , './', {:FancyIndexing => true}
files. Maybe you want to use ajax calls(which is sometimes problematic on IE), trying to use the google maps api, or the FABridge, whatever the reason may be. Well, you can get around this problem easily using this ruby script:
require 'webrick' server = WEBrick::HTTPServer.new :Port => 1234 server.mount "/", WEBrick::HTTPServlet::FileHandler, './' trap('INT') { server.stop } server.start
This runs a web server at http://localhost:1234/ which
mounts the top level directory to your current directory.
Update: Oops, it's not exactly that easy after all. In order to prevent caching - which you will want to do if you are doing development - you will want to write an extra class. The modified script:
require 'webrick' class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler def prevent_caching(res) res['ETag'] = nil res['Last-Modified'] = Time.now + 100**4 res['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0' res['Pragma'] = 'no-cache' res['Expires'] = Time.now - 100**4 end def do_GET(req, res) super prevent_caching(res) end end server = WEBrick::HTTPServer.new :Port => 1234 server.mount "/", NonCachingFileHandler , './' trap('INT') { server.stop } server.start
Not 5 lines anymore, bummer! The code for NonCachingFileHandler was stolen from unittest_js.
Add
, {:FancyIndexing => true} to the end of mount line,as Follows:
server.mount "/", NonCachingFileHandler , './', {:FancyIndexing => true}
相关文章推荐
- Cannot Boot WEBrick: "WARN TCPServer Error: Address already in use "
- 警告: No mapping found for HTTP request with URI [/springmv/iew] in DispatcherServlet with name 'sprin
- [置顶] Spring MVC的简单案例报错:警告: No mapping found for HTTP request with URI [/springMVC/hello2] in DispatcherSe
- [bbk4979]第06集 - Chapter 04- Interacting with Oracle Database Server:SQL Statements in PL/SQL Programs(01)
- https://github.com/mobz/elasticsearch-head#running-with-built-in-server独立的服务启动
- Synchronise time with ntp server in Linux
- 警告: No mapping found for HTTP request with URI [/web002/list.do] in DispatcherServlet 的解决方法
- Tech Tip: Really Simple HTTP Server with Python
- 服务器XMLHTTP(Server XMLHTTP in ASP)基础知识
- [PReact] Reduce the Size of a React App in Two Lines with preact-compat
- No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'projectname'
- Transparent HTTP with Apache Traffic Server
- The server failed to respond with a valid HTTP response
- Use PHP mail function with IIS SMTP Server in Windows for Local Testing Purpose
- The absolute uri: http://struts.apache.org/tags-bean cannot be resolved in either web.xml or the jar files deployed with this application
- No mapping found for HTTP request with URI in DispatcherServlet with name 'XXX'
- mySql主从配置出错:The server is not configured as slave; fix in config file or with CHANGE MASTER TO
- Installing and Configuring the Apache HTTP Server Plug-In (在weblogic 9.x 10.x上配置apache http server 插件)
- jsdom + jQuery in 5 lines with node.js
- How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?