【问题标题】:Cloud9 - Node.js fs can't open an html fileCloud9 - Node.js fs 无法打开 html 文件
【发布时间】:2013-08-08 00:11:40
【问题描述】:

我用 node.js 和 socket.io 做实验,它在本地运行良好。 我可以读取一个 html 文件并为多个用户管理一个交互式按钮。

所以我在 Cloud9 上上传了它,但我遇到了 错误 ENOENT 试图查找 html 文件。 它在根目录中(就像在本地一样),行是 fs.readFile('ex.html' 等...

这是打开 html 文件的 test 代码,我在控制台上有 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);

这是另一个显示空白页的程序(完整)...

服务器:

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);

客户端(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>

我已经在服务器上安装了 socket.io,所有文件都在文件夹 node.js 的根目录下。 我已经问过 Cloud9 但他们说这对他们有用...... 对不起我的英语,如果我是初学者。

感谢您的帮助:)

【问题讨论】:

    标签: html node.js fs


    【解决方案1】:

    我猜你的 ex.html 文件在 node.js 目录中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2015-02-14
      • 2019-01-06
      • 2015-04-02
      • 2013-12-05
      相关资源
      最近更新 更多