2

I have Apache httpd server and node.js. I need to emulate real JSON data which changes every time.

I found, that I can run node.js as server in standalone mode like this:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/')

It's cool, but I can't access it via AJAX request, as there's different port.

Question: How I can, run this script, accessed via different path , e.g. http://localhost/json (not as standanole running at another port)

Thanks,

2 Answers 2

2

You could proxy the connection from the main webserver to the node.js. There's a great tutorial at dailyjs http://dailyjs.com/2010/03/15/hosting-nodejs-apps/ - though it isn't about Apache but Nginx

Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm... Nice one... But I done it via .htaccess RewriteRule ^json.html$ 127.0.0.1:8124 [P]
0

If you are using Node.js you could bypass Apache altogther. I was running basic apps without apache just fine (well to start with anyway), and I had all static files (css js, images etc) on s3/cloudfront.

However now I use NGNIX as a front end (for caching etc) and I essentially load balance incoming to different ports where I have multiple node apps running on different ports. All on 1 box btw.

Apache is not ideal for node.js. In fact if your app is severing basic info you should check this out to see why http://scoop.simplyexcited.co.uk/2010/07/05/node-js-brief-overview-2/

If you are only experimenting with node, you don't really need Ngnix (well not just yet anyway)

PS: as far as your app not returning JSON correctly, I use something more like this.

    res.writeHead(200, { "Content-Type" : "text/plain" });

         myJSON.push({/* some stuff during a loop */})

    res.write(JSON.stringify(myJSON));
    res.end();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.