在apache上使用nodeJS发送文件

时间:2014-08-01 08:17:23

标签: javascript node.js apache

我设置了apache(usbwebserver)和nodejs(两个最新版本)。

我更改了httpd.conf(如此处所述https://stackoverflow.com/a/18604082/1112413

HTTPD.CONF

ProxyPass /node http://localhost:8000/

APP.JS

当前代码:

var app = require('express')();
var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Apache!\n');

}).listen(8000, 'localhost');

//Tried with app.get('/') to    

app.get('/node', function(req, res){
   res.sendfile('index.html');
});

INDEX.HTML

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>

</html>

使用socket.js中的教程

由于res.end中的http.creatserver,我认为它没有加载index.html。 如果我把它带走,它会永远加载。

我做错了什么?只需遵循socket.io(http://socket.io/get-started/chat/

中的教程

我的节点js代码在/ Node中。这是一个问题吗?

IDEA:

我正在尝试创建一个可以使用php和nodejs的应用程序(用于手机屏幕)。

该应用程序有一些明显的CRUD功能。但它也需要聊天。该公司要求我用phonegap写它,因为我们都知道php / js并且没有C#/ Java经验。

所以我去寻找聊天,最好的解决方案似乎是nodejs / socket io,因为它是事件驱动的,因此很快。

1 个答案:

答案 0 :(得分:2)

您使用的是明确的错误:

Express-App实际上是一个可以传递给http.createServer()的对象 - 方法 - 它并没有真正的parellel。

如果你改变它,你的代码应该可以工作:

// Delete the original http.createServer();
app.get('/node', function(req, res){
    res.sendfile('index.html');
});

http.createServer(app).listen(8000, 'localhost');

正如您在示例中所看到的,您必须在启动服务器之前定义路由。