简体   繁体   English

Cloud9-Node.js fs无法打开HTML文件

[英]Cloud9 - Node.js fs can't open an html file

I do experiments with node.js and socket.io and it works fine locally. 我使用node.js和socket.io进行了实验,并且在本地工作良好。 I can read an html file and manage an interactive button for several users. 我可以阅读html文件并为多个用户管理一个交互式按钮。

So i uploaded it on Cloud9 but i have an error ENOENT trying to find the html file . 所以我将其上传到Cloud9上,但是尝试查找html文件时出现错误提示 It's in root (like in local) and the line is fs.readFile('ex.html' etc... 它位于根目录(如本地),其行是fs.readFile('ex.html'等)。

Here is the code of a test to open an html file and i have the enoent error on the console : 这是打开html文件的测试代码,控制台上出现enoent错误

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) : 客户(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. 我已经在服务器上安装了socket.io,所有文件都位于node.js文件夹的根目录中。 I already asked to Cloud9 but they said it works for them... Sorry for my english and if im a beginner. 我已经问过Cloud9了,但是他们说这对他们有用。很抱歉我的英语,如果我是初学者。

Thank you for your help :) 谢谢您的帮助 :)

I guess your ex.html file is in node.js directory. 我猜你的ex.html文件在node.js目录中。

Try fs.readFile('node.js/ex.html', ... 尝试fs.readFile('node.js/ex.html', ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM