Socket.IO/Node.JS execution
Socket.IO/Node.JS execution
我是Node.JS和Socket的新手。所以我决定选择一个简单的聊天客户端作为我的第一个项目。据我所知,它的结构是正确的,但是每当我试图运行index.js
时,就会抛出object expected
错误。有人知道怎么回事吗?(如果有帮助的话,我用的是Windows 8.1)
index . html
<!doctype html>
<html>
<head>
<title>TNjs 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>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
package.json
{
"name": "TNjsChat Client",
"version": "0.0.1",
"description": "A browser based chat system using Node.JS and socket.io, by Touka",
"dependencies": {
"express": "4.10.2",
"socket.io": "1.2.0"
}
}
你需要运行node,而不是让微软IE运行js文件,这是你的屏幕截图所显示的。
确保node.exe在路径的某个地方,然后从index.js目录运行"node index.js",并确保您需要的所有模块(如Express)都正确安装并从index.js目录中可用。
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 使用agility.js进行页面布局和合成
- 使用Clipboard.js复制span文本
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 强制模板刷新ember.js
- 如何编写HTML输入的JS内联
- Angular JS IE9 Hashbang url rewriting
- 使用JS将数组转换为json对象
- Node.js v6.2.0类扩展不是函数错误
- 当js函数's已执行
- 要求未定义JS回调参数
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 如何从Java/scala调用js美化程序
- 如何更改<svg>标记为<img>用js标记
- 如何使用 node.js 比较两个 json 数组
- Passport.js Execution
- JS, how to stop process execution if it's beeing execute
- backbone.js block route execution
- Socket.IO/Node.JS execution