Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling .js and .css in Node.js
    primarykey
    data
    text
    <p>I'm trying to create a simple chat application with node.js and jQuery but I'm getting an error in my console saying '$ is not defined'. I'm following a tutorial where the authr is using jquery without a problem. </p> <p>My chat.js (the server):</p> <pre class="lang-js prettyprint-override"><code>var http = require('http'), sys = require('sys'), fs = require('fs'), ws = require('./ws.js'); var clients = []; http.createServer(function(req,res){ res.writeHead(200,{ 'Content-Type': 'text/html' }); var rs = fs.createReadStream(__dirname + '/test.html'); sys.pump(rs, res); }).listen(4000); ws.createServer(function(websocket){ websocket.on('connect', function(resource){ clients.push(websockets); websocket.write('Welcome'); }); websocket.on('data', function(data){ clients.forEach(function(client){ client.write(data); }); }); websocket.on('close',function(){ var pos = clients.indexOf(websocket); if(pos &gt;= 0){ clients.splice(pos, 1); } }); }).listen(8081); </code></pre> <p>test.html (the client):</p> <pre class="lang-html prettyprint-override"><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;script type="text/javascript" src="jquery-1.7.2.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(e) { ws = new WebSocket("ws://localhost:8080"); ws.onmessage = function(event){ var data = data.replace(/&amp;/g, "&amp;amp;").replace(/&lt;/g,"&amp;lt;").replace(/&gt;/g,"&amp;gt;"); $('#log ul').append('&lt;li&gt;' + data + '&lt;/li&gt;'); } }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="log"&gt;&lt;ul&gt;&lt;/ul&gt;&lt;/div&gt; &lt;div id="console"&gt; &lt;input type="text" id="entry"/&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I was getting errors before I stripped out all the .css and other .js src so I'm pretty sure the problem is with the 'Content-Type' code in the server. </p>
    singulars
    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.
    1. This table or related slice is empty.
    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