开发者

Node.js run script

开发者 https://www.devze.com 2023-01-08 09:26 出处:网络
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:

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('H开发者_开发技巧ello 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,


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


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

精彩评论

暂无评论...
验证码 换一张
取 消