Note that there are some explanatory texts on larger screens.

plurals
  1. POsocket.io and differents folders --- solution found
    primarykey
    data
    text
    <p>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 sample of code from socket.io and everything works well. Now, i want to strcuture my code and folders and i have created a folder "client" to put a fresh new js file client.js with the client code from the example. Here is my architecture </p> <pre><code>/client client.js index.html server.js </code></pre> <p>client.js :</p> <pre><code>var socket = io.connect('http://localhost:80'); socket.on('news', function (data) { alert('sqd'); console.log(data); socket.emit('my other event', { my: 'data' }); }); </code></pre> <p>server.js</p> <pre><code>var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs = require('fs') app.listen(80); function handler (req, res) { fs.readFile(__dirname + '/index.html', 'utf-8', function (err, data) { if (err) { res.writeHead(500); return res.end('Error loading index.html ' + __dirname); } res.writeHead(200, {'Content-Type' : 'text/html'}); res.end(data); }); } io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); </code></pre> <p>index.html</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;&lt;/title&gt; &lt;script type="text/javascript" src="/client/client.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="/socket.io/socket.io.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>When i refresh my browser at localhost:80 i have a error on my client.js :</p> <pre><code>Uncaught SyntaxError: Unexpected token &lt; Resource interpreted as Script but transferred with MIME type text/html </code></pre> <p>It seems that there's a problem to interpret my js file as a js file. I've read some threads on the question but nothing works.</p> <p>Can you help me please ? </p> <p>Thanx :)</p> <hr> <p>Ok i've found a solution... You have to specify the content type for each file request in a static webserver. May be it could help someone. Here is the handler function :</p> <pre><code>function handler (req, res) { var filePath = req.url; if (filePath == '/') { filePath = './client/index.html'; } else { filePath = './client/lib' + req.url; } var extname = path.extname(filePath); var contentType = 'text/html'; switch (extname) { case '.js': contentType = 'text/javascript'; break; case '.css': contentType = 'text/css'; break; } path.exists(filePath, function(exists) { if (exists) { fs.readFile(filePath, function(error, content) { if (error) { res.writeHead(500); res.end(); } else { res.writeHead(200, { 'Content-Type': contentType }); res.end(content, 'utf-8'); } }); } else { res.writeHead(404); res.end(); } }); } </code></pre> <p>Hope this can help someone. I love to post a problem and respond by myself with no help. Somehow it meens that i'm desesperate too fast. And i love to tell my life in a post too :) Ok i'm gonna eat something and drink more coffee !!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload