我正在尝试通过编写聊天程序来学习node.js。我已经有了主要功能,但是当我尝试添加CSS文件时,我对网络代码进行了一些更改以尝试包括静态文件(第11行),现在服务器没有将数据发送到客户端。相反,我收到一个空的响应错误。这是我的服务器代码。
var express = require('express');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var app = express();
http.listen(3000, function(){
console.log('listening on *:3000');
});
app.use(express.static('public'))
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
//connect
var address = socket.handshake.address;
console.log('connection from ' + address);
io.emit('connection from ' + address);
var user = address;
//bring up to speed
var contents = fs.readFileSync(__dirname + '/log.txt', 'utf8');
io.emit('up2speed', contents);
console.log('bringing ' + user + ' up to speed...');
//change name
socket.on('set_user', function(nick){
console.log('changing ' + user + ' to ' + nick + '...')
user = nick;
})
//message
socket.on('send_mes', function(msg){
console.log(user + ' : ' + msg);
io.emit('send_mes', user + ' : ' + msg)
fs.appendFileSync('log.txt', user + ' : ' + msg + '\n');
});
});
我已经尝试过追回自己的脚步,但这只是让我开始了。没有CSS的页面。