目前为止,我们做的服务器没有实际的用处,接下来我们开始实现一些实际有用的功能。
我们要做的是:用户选择一个文件,上传该文件,然后在浏览器中看到上传的文件。
首先我们需要一个文本区(textarea)供用户输入内容,然后通过post请求提交给服务器。
我们在start事件处理器里添加代码,requesthandlers.js修改如下:
复制代码 代码如下:
function start(response) {
console.log(request handler 'start' was called.);
var body = ''+ ''+
''+
''+
''+
''+
''+
''+
''+
''+
'';
response.writehead(200, {content-type: text/html});
response.write(body);
response.end();
}
function upload(response) {
console.log(request handler 'upload' was called.);
response.writehead(200, {content-type: text/plain});
response.write(hello upload);
response.end();
}
exports.start = start;
exports.upload = upload;
通过在浏览器中访问http://localhost:8888/start就可以看到效果了。
接下来我们要实现当用户提交表单时,触发/upload请求处理程序处理post请求。
为了使整个过程非阻塞,node.js会将post数据拆分成很多小的数据块,然后通过触发特定的事件,将这些小数据块传递给回调函数。这里的特定的事件有data事件(表示新的小数据块到达了)以及end事件(表示所有的数据都已经接收完毕)。
我们通过在request对象上注册监听器(listener) 来实现。这里的 request对象是每次接收到http请求时候,都会把该对象传递给onrequest回调函数。
我们把代码放在服务器里,server.js修改如下:
复制代码 代码如下:
var http = require(http);
var url = require(url);
function start(route, handle) {
function onrequest(request, response) {
var postdata = ;
var pathname = url.parse(request.url).pathname;
console.log(request for + pathname + received.);
request.setencoding(utf8);
request.addlistener(data, function(postdatachunk) {
postdata += postdatachunk;
console.log(received post data chunk '+ postdatachunk + '.);
});
request.addlistener(end, function() {
route(handle, pathname, response, postdata);
});
}
http.createserver(onrequest).listen(8888);
console.log(server has started.);
}
exports.start = start;
上述代码做了三件事情: 首先,我们设置了接收数据的编码格式为utf-8,然后注册了“data”事件的监听器,用于收集每次接收到的新数据块,并将其赋值给postdata 变量,最后,我们将请求路由的调用移到end事件处理程序中,以确保它只会当所有数据接收完毕后才触发,并且只触发一次。我们同时还把post数据传递给请求路由,因为这些数据,请求处理程序会用到。
接下来在/upload页面,展示用户输入的内
我们来改一下 router.js:
复制代码 代码如下:
function route(handle, pathname, response, postdata) {
console.log(about to route a request for + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response, postdata);
} else {
console.log(no request handler found for + pathname);
response.writehead(404, {content-type: text/plain});
response.write(404 not found);
response.end();
}
}
exports.route = route;
然后,在requesthandlers.js中,我们将数据包含在对upload请求的响应中:
复制代码 代码如下:
function start(response, postdata) {
console.log(request handler 'start' was called.);
var body = ''+
''+
''+
''+
''+
''+
''+
''+
''+
''+
'';
response.writehead(200, {content-type: text/html});
response.write(body);
response.end();
}
function upload(response, postdata) {
console.log(request handler 'upload' was called.);
response.writehead(200, {content-type: text/plain});
response.write(you've sent: + postdata);
response.end();
}
exports.start = start;
exports.upload = upload;
我们最后要做的是: 当前我们是把请求的整个消息体传递给了请求路由和请求处理程序。我们应该只把post数据中,我们感兴趣的部分传递给请求路由和请求处理程序。在我们这个例子中,我们感兴趣的其实只是text字段。
我们可以使用此前介绍过的querystring模块来实现:
复制代码 代码如下:
var querystring = require(querystring);
function start(response, postdata) {
console.log(request handler 'start' was called.);
var body = ''+
''+
''+
''+
''+
''+
''+
''+
''+
''+
'';
response.writehead(200, {content-type: text/html});
response.write(body);
response.end();
}
function upload(response, postdata) {
console.log(request handler 'upload' was called.);
response.writehead(200, {content-type: text/plain});
response.write(you've sent the text: + querystring.parse(postdata).text);
response.end();
}
exports.start = start;
exports.upload = upload;
好了,以上就是关于处理post数据的全部内容。
下一节,我们将实现图片上传的功能。