以下是我翻译的node入门书"Node For Front-End Developers"
我英文其实挺烂的,但是由于喜欢node.js,自己硬着头皮去一点一点的翻译来看,翻译的过程就想到应该还有和我一样烂的同学们,所以斗胆将翻译出来的东西给大家分享,也希望大牛们帮着看一下翻译的错误,让我们这些英文烂但又积极向上的同学们能够更快的上手node.
文档我会不定期更新,反正是翻译出来一点我就发上来一点,有大牛们指正的地方我也会及时更改.
另外小小的问一下,能不能申请NAE,嘿嘿.
以下是翻译的全文,前面的前沿和教怎么安装node的我就不翻了,大家google一下就可以:
CHAPTER 2 第二章
Serving Simple Content 服务简单的内容
Because serving content is a web server’s reason for being, there are thousands of Node modules available to automate the various ways of doing so, or to wrap that entire set of functions up in robust framework. Working with what Node includes natively, however, provides a beneficial illustration of how it work as a web server, and creating simple applications with its out-of-the-box utilities fairly trivial.
因为Web服务存在的理由是为了提供内容服务,有无数个Node模块提供各种自动化操作的方式,也有包裹整套模块的一个强大的框架. 哪些Node模块包含在本地的工作,下面提供一个有益的说明他是如何工作在web服务器上, 并用现成的工具创造一个简单的应用程序.
Writing a Response Manually 编写一个响应程序
The first thing we’ll do in any web a application we write in Node is to require a module allowing us actually serve a website. Most common server tasks are part of the http or https modules. At minimum, any web application will need to import one of these (or another module which has one or the other as a dependency) using the require function. Node’s built-in dependency management is similar to CommonJs, and require masks the complexity of searching desired module and avoiding redundancy.
var http = require(‘http’);
我们做任何web应用程序的第一件事就是导入一个Node模块让我们成为一个实际服务的网站. 大多数普通的服务器的工作是http或https模块的一部分的功能. 最起码,所有web应用程序都将需要包含其中一个(或者另一个和其他模块存在依赖性的模块)使用的函数. Node内置的依赖关系管理机制和CommonJS相似,搜索想要的模块并掩饰其复杂性和避免重复. var http = require(‘http’);
Once the http module is available, we can create a server and ask it to begin listening for requests. The createServer() function has only one parameter: the callback that will execute whenever a request is received. The listen() function that starts the server can take several arguments,but for a simple server we just need to provide the port and, optionally, the host IP:
var http = require(‘http’);
http.createServer( function (req,res) {
var html = ‘<!doctype html> +
‘<html><head><title>Hello world</title></head>’ +
‘<body><h1>Hello world</h1></body></html>’;
res.writeHead(200, {
// set the type of content we’re returning
‘Content-Type’: ‘text/html’,
//set the length of our content
‘Content-Type’:‘html.length’
});
// end the response,sending it and returning our HTML
res.end(html);
}).listen(8000,‘127.0.0.1);
我们通过这个有效的HTTP模块,我们能够创建一个服务并要求它开始监听请求. createServer()函数仅有一个参数:当接受到请求时将会回调他. listen() 函数开始运行服务器时能够获取几个参数,但是一个简单的服务器我们需要提供端口号及随意的主机IP地址:
var http = require(‘http’);
http.createServer( function (res,req) {
var html = ‘<!doctype html>’ +
‘<html><head><title>Hello world<title></head>’ +
‘<body><h1>Hello World</h1></body></html>’;
res.writeHead (200,{
// 设置返回内容的类型
‘Content-Type’: ‘text/html’,
// 设置内容的长度
‘Content-Type’ : ‘html.length’
});
// 结束服务器的响应,发送它并返回我们HTML页面
res.end(html);
}).listen(8000,‘127.0.0.1’);
The callback in createServer() is listening for a request event, a built-in event type defined by the http module. The event handler receives two arguments: the request and a response object. Since we’re not doing anything dynamic to begin with, we only need to worry about the response we’ll build to send back to the client. The minimum we need to build a response the client can render is the function end(). The end() function will do double duty ending the response and writing content to it, the latter of which can also be done with write(). The writeHead() function creates a header for the file we’re sending to the client, indicating what the browser should do with it. We don’t actually need it here, since it’s mimicking the defaults, but we will later on. The canonical Node Hello World example user these two functions to spit our a little text, but we can go slightly further and return proper HTML.
createServer()函数回调了监听请求事件,它是属于http模块内置的事件类型. 事件处理器接收了两个参数: 接收request和响应response对象. 由于我们不需要做任何事情就能让服务器开始运行,我们只需要担心客户端的响应,所以我们将建立一个响应. 最低限度,我们需要建立一个响应response让客户端能够呈现end()函数. end()函数有两个任务,一个是结束response参数,另外一个是写入内容,写入内容也可以实用write()函数. writeHead()函数创造头文件发送给客户端,指示浏览器可以读取什么类型的内容. 我们这里实际上并不需要他,因为他类似默认的,但是我们将晚些时候用到. 官方Node的Hello World模版使用两个函数去呈现一些简单的文字,但是我们能够进一步的呈现一个完整的HTML文件.
If that worked out, we ought to see a very minimal web page when we start our application, which is as simple as typing node name of file from the command prompt. Depending on how you’ve structured your files, you probably have a single container directory per application, in which case you could name the application file app.js or server.js (or anything else,really,but you’ll see those two frequently). If you’re sharing a directory with other apps or services or just don’t like the convention of giving the main application file a generic name, you can call it something more specific to your app. But let’s say you called it app.js. You starts your application up by returning to the command prompt in your root application directory on your server and typing:
$ node app.js
如果以上都做完了,我们只需要在命令提示符键入一个简单的命令”node 文件的名称”就可以启动我们的应用程序,我们就能看到一个非常简略的应用程序. 这取决于你如何结构化你的文件,你可能每个应用程序都有单个容器目录,在这种情况下你可以命名文件名为app.js或者server.js(或者其他的也可以,但是你将看到这两个是常用的). 如果你共享了其他程序或者服务,或只是不喜欢按照惯例给主要程序文件起通用的名字,你可以给你的程序起更精确的名字. 但是我们现在叫它app.js. 在你拥有权限的程序目录的命令行提示符里键入下面的命令启动你的程序: $ node app.js