NODEJS(2)File Upload Sample with routers and handlers

本文介绍如何使用Node.js实现文件上传功能,包括请求路由、处理器模块及响应处理。通过创建路由和处理器模块,实现了对不同请求路径的响应,并利用非阻塞操作优化了文件上传处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NODEJS(2)File Upload Sample with routers and handlers

What's needed to "route" requests?
We need to be able to feed the requested URL and possible additional GET and POST parameters into our router, and based on these the router then needs to be able to decide which code to execute(a collection of request handlers that do the actual work when a request is received).

We need other modules in Node.js namely url and querystring.

url module provide methods which extract the URL, and querystring module for query string.

We do some changes in server.js as follow, we can get the requestpath in our URL.
var url = require("url");
...snip...
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
We get these messages on console then:
Request for / received.
Request for /luohua received.
Request for /order.do received.

We create a router.js with these content:
function route(pathname){
console.log("About to route a request for " + pathname);
}

exports.route = route;

Pass the function method name to our route and start, refactor server.js as follow:
function start(routeMethod) {
...snip...
routeMethod(pathname);
...snip...

index.js will be changed as follow:
var server = require("./server");
var router = require("./router");

server.start(router.route);

Execution in the kingdom of verbs
Routing to real request handlers
Create a module requestHandlers.js as follow:
function start(){
console.log("Request handler 'start' was called.");
}

function upload(){
console.log("Request handler 'upload' was called.");
}

exports.start = start;
exports.upload = upload;

In JavaScript, objects are just collections of name/value pairs, think of a JavaScript object as a dictionary with string keys.
We use a key/value pair, put the functions in value, and pass this map to our router.
index.js
var requestHandlers = require("./requestHandlers");

var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;

server.start(router.route, handle);

server.js
function start(routeMethod, handleMap) {
...snip...
routeMethod(handleMap,pathname);
...snip...

router.js
function route(handleMap,pathname){
console.log("About to route a request for " + pathname);
if(typeof handleMap[pathname] === 'function'){
handleMap[pathname]();
} else {
console.log("No request handler found for " + pathname);
}
}

Making the request handlers respond
How to not do it
We will write back the content in response
requestHandlers.js
function start(){
console.log("Request handler 'start' was called.");
return "Hello Start";
}

function upload(){
console.log("Request handler 'upload' was called.");
return "Hello Upload";
}

router.js
if(typeof handleMap[pathname] === 'function'){
return handleMap[pathname]();
} else {
console.log("No request handler found for " + pathname);
return "404 Not found";
}

server.js
response.writeHead(200, {"Content-Type": "text/plain"});
var content = routeMethod(handleMap,pathname);
response.write(content);
...snip..

Blocking and non-blocking
We will try to make blocking in request handler, make it wait 10 seconds before returning its "Hello Start" string.

I download the mac version here, Node was installed at /usr/local/bin/node, npm was installed at /usr/local/bin/npm.

And I change the requestHanders.js as follow:
function start(){
…snip…
function sleep(milliSeconds){
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
}

sleep(10000);
return "Hello Start";
}

From the code, we can understand that start will take 10 seconds to sleep. But upload will response immediately.
But we hit the URL http://localhost:8888/start first, and then in another tab, hit the URL http://localhost:8888/upload. But both of the URLs are waiting for 10 seconds.

In fact, Node.js is single-threaded. So we will try to do it no-blocking ways. This way we are saying " probablyExpensiveFunction(), please do your stuff, but the single Node.js thread will not wait right here until you are finished, I will continue to execute the lines of code below you. So would you please take this callbackFunction() here and call it when you are finished doing your expensive stuff.

non-blocking exec module
var exec = require("child_process").exec;

function start() {
console.log("Request handler 'start' was called.");
var content = "empty";

exec("ls -lah", function(error, stdout, stderr) {
content = stdout;
});
return content;
}

But this can only show the empty on the page. Because exec really is non-blocking.

Responding request handlers with non-blocking operations
pass the response to route handler in server.js
…snip…
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");

route(handle, pathname, response);
}
…snip...

router.js will handle the response and pass the response to requestHanders.
…snip…
function route(handle, pathname, response) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response);
} else {
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found");
response.end();
}
}
…snip…

Handle the response in requestHandlers.js

exec("ls -lah", function(error, stdout, stderr) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
response.end();
});
…snip…
function upload(response) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}

This time everything works fine and we can see the file list under that directory.

references:
http://www.nodebeginner.org/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值