初学node.js写了几个文件
第一个是入口文件index.js
var http = require('./server');var rouser = require('./rouse');var head = {};head['/']=rouser.start;head['/start']=rouser.start;head['/upload']=rouser.upload;http.server(rouser.rouse,head);
第二个文件 server.js
var http = require('http');var url = require('url');function server(rouse,head){ function ser(req,res){ var urls = url.parse(req.url).pathname; var aa = rouse(urls,head); res.write(aa); //res.write(req.url); res.end(); } http.createServer(ser).listen('3000');}exports.server=server;
第三个文件 rouse.js文件
function rouse(urls,head){ return head[urls]();}function upload(){ return "Hello upload";}function start(){ return "Hello Start";}exports.rouse = rouse;exports.start = start;exports.upload = upload;
这几个文件 我用cmd运行index.js 运行第一次正常 再运行一次就无法找到了 求高手告知怎么回事 小弟在此谢过
下面是报的错
解决方案
加载页面的时候,浏览器会去请求favicon.ico,head['favicon.ico']为空,所以抛异常。要判断是否为空,然后再执行处理函数。
function rouse(urls,head){ var f = head[urls]; if(typeof f === 'function') { return f(); } return null;}