I have a socket.io project but it doesn't let me use public folder. I would like to launch that hello.js
http://i.stack.imgur.com/ugDZw.png
But i only see
http://i.stack.imgur.com/fZhjJ.png
I tried everything and just reinstalled 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);
You're not using your Express app, but instead a bare http.Server
. If you want to use your Express app (which is where your static
middleware is being used), you should swap out the custom callback for http.createServer()
with app
so that is reads:
var server = http.createServer(app)
Then for Socket.IO integration with Express, you'll need to follow this example .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.