考慮下面的代碼:基本Node.js的問題:處理異常
var meryl = require('meryl'),
merylex = require('meryl-extras'),
staticfile = merylex('staticfile'),
httpserv = require('http');
meryl.p('GET /static/<filepath>', staticfile({root: 'static', path: 'filepath'}));
httpserv.createServer(meryl.cgi()).listen(3000);
,直到您請求不存在的文件,這個偉大的工程。如果我要求/static/non-existent-file
,服務器崩潰,並說:
/home/user/.node_libraries/meryl-extras/lib/plugins/staticfile.js:224 chain(); ^ TypeError: undefined is not a function at CALL_NON_FUNCTION (native) at /home/user/.node_libraries/meryl-extras/lib/plugins/staticfile.js:224:9 at [object Object]. (fs:58:5) at [object Object].emit (events:27:15) at fs:655:12 at node.js:608:9
我如何能趕上這一點,所以我的應用程序不會崩潰?不幸的是,作爲這樣一種新語言,似乎並沒有我能找到的很多例子。
謝謝。
一個解決方案是使用除meryl以外的東西,有很多靜態文件處理庫可以處理丟失的文件等。 – fbstj 2010-09-24 11:03:24