0

I've a setup of apache (usbwebserver) and nodejs (both latest versions).

I changed the httpd.conf (as described here https://stackoverflow.com/a/18604082/1112413)

HTTPD.CONF

ProxyPass /node http://localhost:8000/

APP.JS

Current Code:

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>

Working with the tutorial from socket.js

It doesn't load the index.html I think because of the res.end in http.creatserver. If I take res.end away it keeps forever loading.

What am I doing wrong? Just following the tutorial from socket.io (http://socket.io/get-started/chat/ )

My node js code is in /Node. is this a problem?

IDEA:

I'm trying to create a app (for phonegap) that can use php and nodejs.

The app has some obvious CRUD functions for some stuff. But it needs to have a chat also. The company asked me to write it in phonegap since we all know php/js and don't have C#/Java experience.

So I went searching for a chat and the best solution seemed nodejs/socket io because it is event driven and fast because of that.

Community
  • 1
  • 1
JochemQuery
  • 1,495
  • 2
  • 26
  • 38

1 Answers1

2

You are using express wrong:

The Express-App is actually an object you can pass to the http.createServer()-Method - it doesnt live parellel to it.

Youre code should work if you change it like this:

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

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

As you can also see in the example, you will have to define the routes BEFORE you start the server.

David Losert
  • 4,652
  • 1
  • 25
  • 31
  • @JochemQuery a quick answer would be you are creating all of your routes, e.g. /node, /test etc and setting them against your app object (express). Now at the end of the file you create your HTTP server by running `http.createServer(app)` and passing all the associated routes through to createServer. This then allows you to visit the routes you have setup through the HTTP server you have just created. Just imagine - you (theoretically) can't get into a building with no doors so you need to put the doors in before finishing the building! If you found @Charminbear helpful please mark as correct! – Chris Aug 01 '14 at 08:30
  • You´re welcome. Basically its an easy thing: http.createServer() does nothing else but creating a listener for Port80 and calling the provided function each time a request is incoming. Express provides that function for you and makes it easy for you to instantiate different routes and configurations... – David Losert Aug 01 '14 at 08:32
  • Thanks @Charminbear and Chris. I've to explore more on this subject I see before i'm ready to use it. Just one more question: If I want to run this on mobile (phonegap) should I use a client and server of nodeJS? I edited the idea in the main post. I need to get this clear in my head or will waste a lot of time.. – JochemQuery Aug 01 '14 at 08:38