I guess your ex.html
file is in node.js directory.
Try fs.readFile('node.js/ex.html', ...
質問
I do experiments with node.js and socket.io and it works fine locally. I can read an html file and manage an interactive button for several users.
So i uploaded it on Cloud9 but i have an error ENOENT trying to find the html file. It's in root (like in local) and the line is fs.readFile('ex.html' etc...
Here is the code of a test to open an html file and i have the enoent error on the console :
var http = require('http');
var fs = require('fs');
http.createServer(function(request, response) {
response.writeHead(200, {
'Content-Type': 'text/html'
});
fs.readFile('ex.html', function(err, data){
if(err) throw err;
response.end(data);
});
}).listen(process.env.PORT, process.env.IP);
Here is another program (full) that displays a blank page...
server :
var http = require('http');
var fs = require('fs');
// Creation du serveur
var app = http.createServer(function (req, res) {
// On lit notre fichier app.html
fs.readFile('app.html', 'utf-8', function(error, content) {
res.writeHead(200, {'Content-Type' : 'text/html'});
res.end(content);
});
});
var io = require("socket.io");
io = io.listen(app);
io.sockets.on('connection', function (socket) {
socket.on('joue', function () {
socket.broadcast.emit('joue2');
}); // joue
}); // connection
app.listen(process.env.PORT, process.env.IP);
client (app.html) :
<html><head> <title>Hello</title></head><body>
<button id="button">clic</button>
<div id="render">a</div>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect();
var button = document.getElementById('button');
var render = document.getElementById('render');
button.addEventListener("click", clique, false);
function clique() {
socket.emit('joue');
}
socket.on('joue2', function () {
if (render.innerHTML == 'a') {
render.innerHTML = 'clic multi';
} else {
render.innerHTML = 'a';
}
});
</script></body></html>
I have installed socket.io on the server and all files are in the root of the folder node.js. I already asked to Cloud9 but they said it works for them... Sorry for my english and if im a beginner.
Thank you for your help :)
解決
I guess your ex.html
file is in node.js directory.
Try fs.readFile('node.js/ex.html', ...