socket.io with Express 4.15 template from WebStorm not working

会有一股神秘感。 提交于 2019-12-25 18:43:32

问题


I'm trying to setup a Node.js server with socket.io. I'm using the latest template given by WebStorm template.

My step is first add socket.io to my language and framework - Node.js and NPM. Here is my setting

Then in my "bin/www" I add the below

var io = require('socket.io')(server)

And add this to bottom of the bin/www file (after server has start listening)

io.on("connection",(socket)=>{
  console.log("user connected" + socket.id)
})

Then I modify my index.jade as below

extends layout

block content
  h1= title
  p Welcome to #{title}

  li what the ul1
  li what the ul2
  script.
    src="/socket.io/socket.io.js"
  script.
    var socket = io();

Run the server. visit the page and this is what I get

I get the error

Uncaught ReferenceError: io is not defined
    at localhost/:1

Anyone know what wrong with my implementation?

Edit 1: I can get to the socket.io.js by my browser


回答1:


I guess your browser can't find the library. You could include it by adding

src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.1/socket.io.js"

to your file.



来源:https://stackoverflow.com/questions/44199878/socket-io-with-express-4-15-template-from-webstorm-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!