2015-06-02 82 views
0

我有一個socket.io項目,但它不讓我使用公用文件夾。我想啓動該hello.jsexpress.static在node.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

你不使用您的快速應用程序,而是僅使用http.Server。如果您想使用您的快速應用程序(這是正在使用您的static中間件是哪裏),你應該換出定製的回調http.createServer()app使得讀取:

var server = http.createServer(app) 

那麼對於Socket.IO與整合快遞,您需要按照this example