socket.io and differents folders — solution found

前端 未结 5 1491
轮回少年
轮回少年 2020-12-18 09:39

I\'m new to socket.io and i already have a problem, minor i think. I have installed node.js properly and socket.io too with npm. Then just for testing i cut and paste a sam

5条回答
  •  甜味超标
    2020-12-18 10:03

    And you must made fs.readFile wrapped by a closure, otherwise some file (especially the last file) will be read more than once, and others will not be read at all. And the contentTypewill not be set as you wish. This is because of the callback strategy used by fs.readFile. The problem does not appear when the html file just load one external file, but as the external files(css, js, png) loaded more than one it will appear as i pointed out above. (I came upoon this by myself)

    So your code should make a little change as follows:

    ;(function (filename, contentType) {
    
        fs.readFile(filename, function(err, file) {
            // do the left stuff here
        });
    
    }(filename, contentType)); 
    

提交回复
热议问题