Nodejs - socket.io and jquery

0

Good afternoon

I'm following the socket.io start tutorial and it's all working, but I do not understand why jquery only works if calling the script in html

I tried to install by npm and it does not work (the message does not log in)

I thought that package.json was right so I did not have to call the script in html

Or to use by npm I should have taken the html script by putting in a file the part?

Can you explain better how this works?

<!doctype html>
<html>
  <head>
    <title>Olá, Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
    <script src="/socket.io/socket.io.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script><script>$(function(){//loadsocket.io-clientvarsocket=io();$('form').submit(function(){socket.emit('chatmessage',$('#m').val());$('#m').val('');returnfalse;});});</script></head><body><ulid="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>
</html>
    
asked by anonymous 02.07.2018 / 17:40

1 answer

1

Since you are using node.js one option is to take it out of the html and add it to a separate file and create a variable with the symbol $ used in jquery (be sure to include jquery in your package.json or run the command npm install --save jquery

var $ = jQuery = require('jquery')(window);
    
02.07.2018 / 18:19