ITeye博客

Node.js

共 5 篇文章

用Connect——HTTP中间件模块实现一个web服务器

虽然Node.js中的http核心模块可以用来实现web服务器,但要做很多工作,例如要检测每个文件的content type 因此我们决定用Connect模块,非常流行的Express web框架就是建立在Connect之上的 一个快速的静态web服务器: var connect = require('connect'); connect.createServer( connect.static(__dirname) ).listen(8080); 要运行此代码,你需要安装Connect,当然你可以直接执行npm install connect,但最好是在你的项目目录 中新建一个packag…

2017-03-22 21:22 · Node.js

使用Node包管理器

Node中的内置模块被称为Node核心。虽然Node的核心囊括了许多有用的功能,你同样很有可能需要用到社区创建的一些功能。 下面将学习如何利用 npm 来查找插件、查看插件的文档,以及插件的源码。 我系统上没有npm: 如果你已经安装了Node,则npm应该也装好了。可以在命令行输入 npm 来验证一下。如果不行,安装方法如下: cd /tmp git clone git://github.com/isaacs/npm.git cd npm sudo make install 安装好npm后,在命令行上输入 npm -v 可以验证版本号。 1、查找包 用的是npm的search命令。如,你想…

2015-12-31 10:42 · Node.js

运行多个 Node 服务器

购买若干服务器很容易,然后在这些服务器上运行一些 Node 进程。But how can these distinct servers be coordinated such that they form part of a single application? One aspect of this problem concerns clustering multiple identical servers around a single entry point. How can client connections be shared arcoss a pool of servers? …

2015-08-31 10:12 · Node.js · nginx · node · Reverse Proxy

Your First Web Server

现在,你已准备好编写有点有趣的东西了,就比如我们要写一个小的web server。幸运的是,Node 让这样的任务变得很简单。把下面的代码保存到一个叫 web.js 的文件中: var http = require("http"); function process_request(req, res) { var body = 'Thanks for calling!\n'; var content_length = body.length; res.writeHead(200, { 'Content-Length': content_length, 'Content-Type': 'tex…

2015-07-31 23:08 · Node.js · node.js

The Node Shell

Node shell 常常也叫做 Node REPL——REPL代表Read-Eval-Print-Loop。 启动:在 shell 中输入 node 就可以了 client:node marcw$ node > 下面输入了一些代码: > console.log("Hello World"); Hello World! undefined > 输出的第一行是执行的代码的结果。这里,你用 Node 的全局变量 console 和它的 log 函数打印出 Hello World! 输出的最后总是之前代码的 resulting value。每一个语句、函数调用或表达式都带有一个值,该值会被打印在 …

2015-07-31 22:28 · Node.js · node.js