2013-03-24 109 views
0

我怎么能有一个错误HANDELING时,当我加载的Node.js 模块假设是这样的NodeJS异常处理加载mysql模块。 是这样的:加载模块

try: 
mysql = require('mysql') ; 
catch(e): 
console.log("there is a error loading module X"); 

问题的另一部分是即时寻找一种方式来加载基于主机操作系统上的模块。例如在Linux上加载一些模块,在Windows上加载其他模块。 谢谢

回答

2

这只是正常:基于OS

try { 
    var mysql = require('mysql'); 
} catch(e) { 
    console.log('error loading mysql module', e); 
}; 

加载模块可与检查os.platform()来完成:

var platform = require('os').platform(); 
if (platform === 'linux') { 
    ... 
} 
else 
if (platform === 'windows') { // not sure if its called `windows` because I don't have a Windows machine 
    ... 
}; 
+1

其顺便说一下windows的win32 – user1229351 2013-03-24 09:48:53

0

用你表示?如果是的话,你可以尝试错误500处理:

app.use(function(err, req, res, next){ 
    console.log("this is an error"); 
});