支持node.js的快速啟動Web服務調試工具:serve2
與 Python 的 SimpleHTTPServer,Node.js 的 serve 不同,serve2 是一個支持動態文件的工具,前者們只支持靜態文件,而serve2支持編寫代碼動態處理和返回http請求。
常常我們可能會有這樣的需求場景:無論是在開發時還是嘗試某個框架時,往往需要處理請求參數、Cookies、Http headers等,這樣簡單的靜態文件服務器便無法滿足這種需求了。
于是serve2便應運而生了,基于Tj的serve開發,使用起來跟一般的靜態文件服務器相同,執行一條命令即可。詳細的用法介紹如下。
1. 下載安裝
npm install -g serve2
2. 使用運行
λ serve2 --help Usage: serve2 [options] [dir] Options: -h, --help output usage information -V, --version output the version number -a, --auth <user>:<pass> specify basic auth credentials -F, --format <fmt> specify the log format string -p, --port <port> specify the port [3000] -H, --hidden enable hidden file serving -S, --no-stylus disable stylus rendering -J, --no-jade disable jade rendering --no-less disable less css rendering -I, --no-icons disable icons -L, --no-logs disable request logging -D, --no-dirs disable directory serving -f, --favicon <path> serve the given favicon -M, --mocks <path> mock files directory --cookies add cookies parse support -C, --cors allows cross origin access serving --compress gzip or deflate the response --exec <cmd> execute command on each request
3. 模擬后端
準備一個文件夾,如mock,然后啟動的時候用-M參數指定。此目錄下即為動態文件目錄,可以模擬后端請求處理。如:
// mock/test.js module.exports = function(req, res, next) { var query = req.query; var reqBody = req.body; // ... res.end(query.hi); }
則請求:
都將返回hello。js文件中的函數即是connect的中間件函數形式。
另外,也可以是json或其它文本文件, 甚至可以是目錄:
4. 代理功能
當某個HTTP請求我們希望轉發給服務器來處理時,可以在工作目錄下創建一個文件名為proxylist.json,內容格式如下:
{ "/": "http://www.baidu.com", "/t/180521": "https://www.v2ex.com" }
啟動serve2后訪問http://localhost:3000/,請求即會被proxy到baidu.com。
本文由用戶 cbgd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!