Page 36 - Node.js开发指南
P. 36

3.1  开始用 Node.js 编程    27


                                                                                                      1





                                                                                                      2






                                                                                                      3
                                             图3-1  Node.js 与 PHP 的架构

                   好了,回归正题,让我们创建一个 HTTP 服务器吧。建立一个名为 app.js 的文件,内容
               为:                                                                                     4

                   //app.js

                   var http = require('http');
                                                                                                      5
                   http.createServer(function(req, res) {
                     res.writeHead(200, {'Content-Type': 'text/html'});
                     res.write('<h1>Node.js</h1>');
                     res.end('<p>Hello World</p>');
                   }).listen(3000);                                                                   6
                   console.log("HTTP server is listening at port 3000.");
                   接下来运行 node app.js命令,打开浏览器访问 http://127.0.0.1:3000,即可看到图3-2
               所示的内容。
                                                                                                      7






                                                                                                      8




                                                                                                      9





                                                                                                      10
                                          图3-2 用 Node.js 实现的 HTTP 服务器
   31   32   33   34   35   36   37   38   39   40   41