javascript - Prevent favicon.ico from making a second request - Node.js -
i'm trying prevent favicon.ico form making second request when socket connects.
here server:
var io = require('socket.io'), connect = require('connect'); var app = connect() .use(connect.static('public')) .use(function(req, res, next){ if (req.url === '/favicon.ico') { res.writehead(200, {'content-type': 'image/x-icon'} ); next(); } }) .use(function(req, res){ res.end('stupid favicon...'); }).listen(3000); var game = io.listen(app); game.sockets.on('connection', function(socket){ socket.emit('entrance', {message: 'welcome game!'}); socket.on('disconnect', function(){ game.sockets.emit('exit', {message: 'a player left game...!'}); }); game.sockets.emit('entrance', {message: 'another gamer online!'}); });
this not seem work. no errors, when 1 socket connects, 2 images loaded client side, makes seem there still 2 requests happening.
so code wrong, or on right track? because no matter console.log()
in server-code, nothing printed console.
edit: client side
var socket = io.connect('http://localhost:3000'); socket.on('entrance', function(data){ console.log(data.message); var num = (count > 0) ? count : ''; console.log("hero" + num); var hero = new car("hero" + num); hero.init(); count++; });
count
global. have (for 2 images), 1 #hero
, #hero1
. when 1 player connects both images loaded.
you're not supposed call next
when want respond request. generally, it's not valid writehead()
, call next
. middleware expects work response has not written network yet.
the reason "stupid favicon" middleware runs you're calling it invoking next
in first one. need res.writehead()
, res.end()
or call next
.
function(req, res, next){ if (req.url === '/favicon.ico') { res.writehead(200, {'content-type': 'image/x-icon'} ); res.end(/* icon content here */); } else { next(); } }
alternatively, use the builtin favicon middleware, important things setting proper cache-control
headers.
Comments
Post a Comment