Why do I get a Bad request when I run server.js and client.js and access localhost?

418 views Asked by At

I am new to node.js

I am trying to use the pub/sub faye nodemodule to create a publish/subscribe web application.

I have the following files and folders: My dependencies in the node_modules folder, a sever.js file, and a client.js

My server.js contains the following code:

var http = require('http'),
faye = require('faye');

var server = http.createServer(),
bayeux = new faye.NodeAdapter({mount: '/'});

bayeux.attach(server);
server.listen(8000);

As for my client.js file, it contains the following code:

var faye = require('faye')
var client = new faye.Client('http://localhost:8000/');

client.subscribe('/messages', function(message) { 
alert('Got a message: ' + message.text); 
});

client.publish('/messages', {
text: 'HAI!'
})

I go to my terminal and run node server.js and node client.js

When I go to my browser and run localhost:8000, all I get is a Bad request.

Why am I getting this? I am suspecting I don't have any page to display something. Could someone please point out to me what I am missing.

Thanks.

1

There are 1 answers

2
mscdex On BEST ANSWER

You're getting that error because you've mounted Faye at /. So if you try browsing to /, the server is expecting a Faye client, not a normal HTTP request.

There is a browser client example that shows how to connect with Faye mounted at /faye. You can also pass in a request handler into http.createServer() that allows you to handle the request before Faye does, in case you want to respond to the particular request.