我有一个socket.io项目,但它不允许我使用公共文件夹。我想启动hello.js
http://i.stack.imgur.com/ugDZw.png
但我只看到
http://i.stack.imgur.com/fZhjJ.png
我尝试了一切,只是重新安装了node.js
var express = require('express');
var app = express();
var http = require("http");
var io = require('socket.io');
var url = require('url');
var fs = require('fs');
app.use(express.static(__dirname + '/public'));
var server = http.createServer(function(request, response){
console.log('Connection');
var path = url.parse(request.url).pathname;
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');
response.end();
break;
case '/socket.html':
fs.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 404");
}
else{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
}
response.end();
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
response.end();
break;
}
});
server.listen(8001);
答案 0 :(得分:0)
您没有使用Express应用,而是使用裸http.Server
。如果您想使用Express应用程序(这是您使用static
中间件的地方),您应该将http.createServer()
的自定义回调换成app
,如下所示:
var server = http.createServer(app)
然后,为了与Express集成Socket.IO,您需要关注this example。