So, I have set up a server with Node.js. The only problem is that the client-side page doesn't seem to be able to be accessed. This is my server-side code:
var HTTP = require("http");
var IO = require("socket.io");
var Server = HTTP.createServer(function(Request, Response){
Response.writeHead(200, {"Content-Type": "text/html", "Location": __dirname + "/Chat.html"});
Response.end("<h1> Hello!</h1>");
}).listen(8080);
var Socket = IO.listen(Server);
var UsersOnline = {};
Socket.on("connection", function(socket){
socket.on("ENTER", function(){
console.log("A");
});
});
This is my client-side code:
<script src="/socket.io/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
$(function(){
Console.log("A");
var socket = new io.Socket("localhost", { "port":8080});
socket.connect();
socket.on("connect", function(){
socket.emit("ENTER");
console.log("A");
});
});
</script>
<h1>ABC</h1>
"ABC" nor "A" appear on the page. I have no idea why this is happening. Is there any way to use separate pages as client-side scripts?
You are trying to send your html through headers. To send static html, simply get the file and send it separately. Use this example
var HTTP = require("http");
var IO = require("socket.io");
var fs = require('fs');
var index = fs.readFileSync('Chat.html');
var Server = HTTP.createServer(function(Request, Response){
Response.writeHead(200, {"Content-Type": "text/html", "Location": __dirname + "/Chat.html"});
Response.end(index);
}).listen(8080);
var Socket = IO.listen(Server);
var UsersOnline = {};
Socket.on("connection", function(socket){
socket.on("ENTER", function(){
console.log("A");
});
});