【发布时间】:2016-09-18 14:16:37
【问题描述】:
我在这里尝试一个愚蠢的简单聊天应用程序:http://socket.io/get-started/chat/
一切都好,除了我想我会将 index.html 文件中的 css 和 js 拆分为单独的文件。所以我有一个看起来像这样的项目文件夹:
/myProject
index.js
index.html
/node_modules
/express
/socket.io
所以在 myproject 文件夹中,我创建了 client 文件夹并将 app.css 和 app.js 放在那里:
/myProject
/client
app.css
app.js
并切换了我的html:
<link rel="stylesheet" href="/client/app.css" />
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/client/app.js"></script>
我重新启动节点服务器并运行索引,但是我的 chrome 检查器在 /client 文件上有 404。
我尝试将客户端文件夹移动到 node_modules 文件夹中(认为这一定是 html 从中获取 socket.io 引用的地方),但没有这样做。
当节点服务器启动时,它从哪个本地文件夹提供服务(此处使用 Windows),以及如何添加其他客户端文件?我不应该在包依赖项中列出所有这些吗?
根据下面的答案,是的,使用 express (4.10.2)
得到错误:
参考错误:快递没有定义
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket) {
console.log('user connected');
socket.on('disconnect', function() {
console.log('user disconnected');
});
socket.on('chat message', function(msg) {
console.log('message: ' + msg);
});
});
http.listen(3000, function() {
console.log('listening on *.3000');
});
【问题讨论】:
标签: node.js