Development environment where PHP and Nodejs coexist

Source: Internet
Author: User
Tags php language php server node server

1 Toss php Nodejs together

Nodejs Of course is very fire, like a fire, but must admit to build a front-end demo development environment or PHP, Windows can be very integrated suite, such as http://www.apachefriends.org/zhcn/ xampp.html, the PHP language itself is a powerful template, when writing a demo, the common tail, can be introduced through include and require, can be built into the loop to generate some DOM structure, in contrast, node is not so convenient.

Since PHP is so convenient, but why still want to use with node, really a bit for use and use ...

2 How?

If you have Python installed, you can execute a simple command immediately, and a simple development server is done.

Python-m Simplehttpserver

But PHP, until php5.4, supported similar features.

$ php-s 0.0.0.0:8000php 5.4.0 Development Server started at Tue 23:21:50 2012Listening on 0.0.0.0:8000document Roo T is/home/tompress ctrl-c to quit.

PHP itself can be a server, Nodejs can also rack a server, then there is no Apache, Nginx

The basic idea is that node opens a server as the foreground, listening to 80 ports, similar to Apache's role, PHP opens a server in the background to run. The node service forwards the HTTP request to the PHP server for execution, which is returned to the node server when the execution is completed, and the node server returns to the browser

node is assuming an intermediate proxy role.

var fs = require (' FS '), HTTP = require (' http '), spawn = require (' child_process '). Spawn, Phpserver;phpserver = Spa WN (' php ', ['-s ', ' 0.0.0.0:8000 ']);p hpserver.stdout.on (' Data ', function (data) {Console.log (' stdout: ' + data);}); Phpserver.stderr.on (' Data ', function (data) {Console.log (' stderr: ' + data);}); Phpserver.on (' Exit ', function (code) {Console.log (' child process exited with code ' + code ');}); Process.on (' Exit ', function () {Phpserver.kill (' SIGHUP ');});  function HandleRequest (request, Response) {var headers = {};  for (var x in request.headers) {headers[x] = request.headers[x];  } headers[' Content-type ']= ' application/x-www-form-urlencoded '; var proxy_request = Http.request ({host: ' localhost ', port:8000, Method:request.method, Path:request.url, he  Aders:headers}); Proxy_request.on (' response ', function (proxy_response) {Response.writehead (proxy_response.statuscode,proxy_    Response.headers); Proxy_response.on (' Data ', function (chunk) {ResponSe.write (new Buffer (chunk));    });    Proxy_response.on (' End ', function () {response.end ();  });  });  Request.on (' Data ', function (chunk) {Proxy_request.write (new Buffer (chunk));  });  Request.on (' End ', function () {proxy_request.end (); });} Http.createserver (HandleRequest). Listen (80);

Save the above file as Server.js and then execute it on the command line

Node Server.js

A node and PHP mashup server was built.

3 Attention points3.1 Request and response data must be a buffer object
Response.Write (New buffer (chunk)) Proxy_request.write (new buffer (chunk));
3.2 To pass the form data, you need to set the header
headers[' Content-type ']= ' application/x-www-form-urlencoded ';

Author:tom

Date:2012-08-21 23:44:28 CST

HTML generated by Org-mode 6.33x in Emacs 23

Development environment where PHP and Nodejs coexist

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.