首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >聊天应用程序不工作怎么解决?

聊天应用程序不工作怎么解决?
EN

Stack Overflow用户
提问于 2018-06-14 04:44:05
回答 2查看 0关注 0票数 0

我制作一个聊天应用程序。当我运行这个应用程序时,在输入框中输入一些内容并发送,没有任何反应。

错误:

我在控制台中遇到错误

代码语言:javascript
复制
localhost/:1 Refused to apply style from 'http://localhost:3001/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
app.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
angular2.js:679 Uncaught ReferenceError: System is not defined
    at angular2.js:679
app.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
localhost/:1 Refused to execute script from 'http://localhost:3001/js/app.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
localhost/:1 Refused to apply style from 'http://localhost:3001/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

index.html

代码语言:javascript
复制
<!doctype html>
<html ng-app="myApp">
  <head>
    <title>Socket.IO chat</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-alpha.20/angular2.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js">
    </script>
    <script src="js/app.js"></script>
  </head>
  <body ng-controller="mainController">
    <ul id="messages"></ul>
    <div>
      <input id="m" ng-model="message" autocomplete="off" />
      <button ng-click="send()">Send</button>
    </div>
  </body>
</html>

server.js

代码语言:javascript
复制
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
  res.sendfile('index.html');
});
io.on('connection', function(socket){
  console.log('user connected');
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
  socket.on('disconnect', function(){
    console.log('user disconnected');
  });
});
http.listen(3001, function(){
  console.log('listening on *:3001');
});

app.js

代码语言:javascript
复制
var app=angular.module('myApp',[]);
app.controller('mainController',['$scope',function($scope){
 var socket = io.connect();
 $scope.send = function(){
  socket.emit('chat message', $scope.message);
  $scope.message="";
 }
 socket.on('chat message', function(msg){
  var li=document.createElement("li");
  li.appendChild(document.createTextNode(msg));
  document.getElementById("messages").appendChild(li);
 });
}]);

style.css文件

代码语言:javascript
复制
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
div { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
div input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
div 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; }
EN

回答 2

Stack Overflow用户

发布于 2018-06-14 12:44:35

我猜你的问题来源:

  • 使用Angular2
  • Socket.io版本(它可能发生了重大更改)
  • 文件夹布局
票数 0
EN

Stack Overflow用户

发布于 2018-06-14 13:48:09

你需要学习调试和解决问题。开发人员不存在“没有任何反应”。尝试通过减少小问题中的问题来解决问题。例如问自己这些问题:

1.期望的输出是什么(显示消息)2.我的输入是什么(点击按钮)3.我的控制台中是否有错误?

答案告诉你在代码中查找的地方。例如,从想要从按钮点击获取回调的位置开始。在那里添加一些记录。你看到什么了吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100004910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档