javascript - 使用 c9.io 设置 Node js 服务器

标签 javascript c9.io

我很难尝试设置节点服务器来在 c9.io 上运行一个简单的应用程序。首先,服务器在本地主机上运行没有问题,但由于某种原因,c9 渲染了 html,但不使用 socket.io.. 这是我的代码:

应用程序.js

const http = require('http');
const socketio = require('socket.io');
const express = require('express');

const app = express();
const server = http.createServer(app);
const io = socketio.listen(server);

app.get('/', (req, res) => res.sendFile(`${__dirname}/client/index.html`))

io.on('connection', socket => {
    socket.on('move', data => {
        io.emit('move', data)
    })
})

server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", () => {
  const addr = server.address();
  console.log("Chat server listening at", `${addr.address}:${addr.port}`);
});

index.html

<!doctype html>
<html lang="en" ng-app>
  <head>
    <title>Chat Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/bootstrap-responsive.min.css">
    <style>
      body {
        padding-top: 60px;
      }
    </style>
    <script>
    const socket = io.connect('https://socket-alexcnu.c9users.io/');   /* global io $*/ 

    $(document).on('mousemove', e => {
        let data = {x: e.pageX - 10 , y: e.pageY - 10}
        $('div').css({
           left:  e.pageX - 10,
           top:   e.pageY - 10
        });

        socket.emit('move', data)
    });

    socket.on('move', data => {
        console.log(data.x);
         $('div').css({
           left: data.x,
           top: data.y
        });
    })

    </script>
  </head>
  <body>
   <div style = 'height: 20px; width: 20px; background: orange; position:absolute; float:left'></div>

  </body>
      <script src="/socket.io/socket.io.js"></script>
    <script src="/js/jquery.min.js"></script>
</html>

c9 控制台

alexcnu:~/workspace $ node app
   info  - socket.io started
Chat server listening at 0.0.0.0:8080
   debug - served static content /socket.io.js

Google Chrome 控制台

https://socket-alexcnu.c9users.io/css/bootstrap-responsive.min.css Failed to load resource: the server responded with a status of 404 (Not Found)
https://socket-alexcnu.c9users.io/css/bootstrap.min.css Failed to load resource: the server responded with a status of 404 (Not Found)
socket-alexcnu.c9users.io/:14 Uncaught ReferenceError: io is not defined
2https://socket-alexcnu.c9users.io/js/jquery.min.js Failed to load resource: the server responded with a status of 404 (Not Found)
https://socket-alexcnu.c9users.io/css/bootstrap-responsive.min.css Failed to load resource: the server responded with a status of 404 (Not Found)
https://socket-alexcnu.c9users.io/css/bootstrap.min.css Failed to load resource: the server responded with a status of 404 (Not Found)

最佳答案

以下内容对我有用:

<!doctype html>
<html lang="en" ng-app>
  <head>
    <title>Chat Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
    <style>
      body {
        padding-top: 60px;
      }
    </style>
    <script>
    const socket = io.connect('https://socket-alexcnu.c9users.io/');   /* global io $*/ 

    $(document).on('mousemove', e => {
        let data = {x: e.pageX - 10 , y: e.pageY - 10}
        $('div').css({
           left:  e.pageX - 10,
           top:   e.pageY - 10
        });

        socket.emit('move', data)
    });

    socket.on('move', data => {
        console.log(data.x);
         $('div').css({
           left: data.x,
           top: data.y
        });
    })

    </script>
  </head>
  <body>
   <div style = 'height: 20px; width: 20px; background: orange; position:absolute; float:left'></div>

  </body>
</html>

关于javascript - 使用 c9.io 设置 Node js 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39434446/

相关文章:

javascript onclick 函数不起作用

javascript - 上一个完成时 jQuery 启动函数

ruby-on-rails - 使用 c9 时如何在 Rails 中设置 Firefox Binary 的路径?

javascript - 按照说明操作后,MongoDB 将无法连接到 C9

CSS 样式表无法连接到 C9 平台上的 node.js 页面

javascript - 将高斯模糊应用到 Div

javascript - 将鼠标悬停在 HTML/JS/JQUERY 中的列表上时弹出图像

javascript - 文本区域只读自动高度(填充所选日期)

javascript - 我无法通过 socket.io 获取实时数据到客户端并显示在页面上

c9.io - Cloud9 IDE 带有 Vue.js vue 组件文件?