每当我们看完文档总会有种错觉,这特么太简单了,是我智商所属范围么!!!
但是当我们开始写bug的时候,没错就是你的病情,说你呢
是时候来点非概念的了,否则热心满满的观众就不乐意了。
目标:
上传文件、文件夹
给你三秒钟,思考下可能用到哪些知识点
前期准备
1.1 整体流程
1. index.js模块: 程序入口, 有服务器(server.js)和路由(route.js)和处理(handler.js)模块组成; 2. server.js模块: 专门处理不同路由, 并做相应的处理; 3. route.js模块: 请求的地址, 处理方法, 响应, 请求; 4. handler.js模块: 封装不同的方法,交由路由route.js模块来选择调用; 5. HTML文件: 存在HTML文件; 6. package.json模块: CommonJS规定的规范;
我想这个不同于理论的实践,会慢慢为接下来打下坚实的基础的。
1.2 模块包
formidable是一个用于处理文件、图片、视频等数据上传的模块,支持GB级上传数据处理,支持多种客户端数据提交。有极高的测试覆盖率,非常适合在生产环境中使用。
npm配置安装
npm install formidable@latest
util是一个Node.js核心模块,util模块设计的主要目的是为了满足Node内部API的需求。其中包括:格式化字符串、对象的序列化、实现对象继承等常用方法。要使用util模块中的方法,只需require('util')引入即可。
1.3 package.json文件
package.json:定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称、版本、许可证等元数据)。npm install命令
根据这个配置文件,自动下载所需的模块.
1. scripts指定了运行脚本命令的npm命令行缩写,比如start指定了运行npm run start时,所要执行的命令。 2. dependencies字段指定了项目运行所依赖的模块; 3. devDependencies指定项目开发所需要的模块。
指定版本:比如1.2.2,遵循“大版本.次要版本.小版本”的格式规定,安装时只安装指定版本。 波浪号(tilde)+指定版本:比如~1.2.2,表示安装1.2.x的最新版本(不低于1.2.2),但是不安装1.3.x,也就是说安装时不改变大版本号和次要版本号。 插入号(caret)+指定版本:比如ˆ1.2.2,表示安装1.x.x的最新版本(不低于1.2.2),但是不安装2.x.x,也就是说安装时不改变大版本号。 需要注意的是,如果大版本号为0,则插入号的行为与波浪号相同,这是因为此时处于开发阶段,即使是次要版本号变动,也可能带来程序的不兼容。 latest:安装最新版本。
例子如下:
{
"name": "upload",
"version": "1.0.1",
"description": "文件上传下载",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "hhw",
"license": "ISC"
//依赖模块
"dependencies": {
"formidable":"formidable@1.0.17",
},
}
第二部分 代码实现部分
2.1 服务器server.js代码实现
//引入http utl 模块
var http = require('http');
var url = require('url');
//程序入口, 选择路由, 并做相应的处理
function start(route, handler){
//选择路由
function onRequest(request, response) {
//url.parse(): 输入 URL 字符串,返回一个对象。
//对象中 pathname 所代表的路径
var pathname = url.parse(request.url).pathname;
route(pathname, handler, response, request);
}
//服务器对不同的路由做出不同的响应
http.createServer(onRequest).listen(3000);
}
//导出接口
exports.start = start;
2.2 路由选择代码
//具体处理路由的功能模块
//请求的地址, 处理方法<upload download...>, 响应, 请求
function route(pathname, handler, response, request){
//如果请求的方法是存在的函数就返回交给服务器处理
if (typeof handler[pathname] === 'function'){
return handler[pathname](request, response);
} else{
//否则返回404
response.writeHead(404, {'Content-Type': 'text/html'});
response.write('404 Not Found!');
response.end();
}
}
exports.route = route;
2.3 具体处理不同需求的代码实现
node-formidable
//解析字符串, fs模块, 格式化模块 var fs = require('fs'), formidable = require('formidable'), util = require('util'), http = require('http'); const path = require('path'); //程序入口展示HTML文件 function home(request, response){ //加载本地页面HTML fs.readFile('./index.html', function(err, data) { response.writeHead(200, {'Content-Type': 'text/html'}); response.write(data); response.end(); }); } //formidable是一个用于处理文件、图片、视频等数据上传的模块,支持GB级上传数据处理,支持多种客户端数据提交。有极高的测试覆盖率,非常适合在生产环境中使用。 function uploads(request, response) { //form表单 var form = new formidable.IncomingForm(); //保留后缀 form.keepExtensions = true; form.encoding = 'utf-8'; //上传的数据保存的路径 form.uploadDir = './'; //该方法会转换请求中所包含的表单数据,callback会包含所有字段域和文件信息 // fields 是普通表单数据 // files 是文件数据 form.parse(request, function(err, fields, files) { response.writeHead(200, {'Content-type': 'text/plain'}); //上传文件的名称 var filename = files.upload.name; var path = files.upload.path; //更改名称 fs.rename(path, form.uploadDir + filename); //响应 格式化打印 response.end(util.inspect(form)); }); } function show (response, request) { console.log('show module'); fs.readFile ('/tmp/test.png ', 'binary', function (error, file) { if (error) { response.writeHead(200, {'Content-Type': 'text/html'}); response.write(error); response.end(); } else { response.writeHead(200, {'Content-Type': 'image/png'}); response.write(file, 'binary'); response.end(); } }); } function downloads(requset, response){ //node官网图标 var URL = "http://nodejs.cn/static/images/logos/nodejs-green.png"; //request request .get(URL) //监听response .on('response', function(res) { console.log(res.statusCode); // 200 console.log(res.headers['content-type']); // 'image/png' console.log(res.headers['content-length']); //打印接受的数据大小 res.on('data', function(data){ console.log('接受到数据' + data.length ) }) }) //监听错误信息 .on('error', function(error){ consloe.log(error); }) //以流的形式写入创建的test.png文件 .pipe(fs.createWriteStream('test.png')); response.writeHead(200); response.write('请前往同级目录下查看下载内容'); response.end(); } exports.home = home; exports.upload = uploads;
exports.download = downloads;
2.4 程序入口调用<待补充>
//引用模块
var server = require('./server');
var router = require('./router');
var handler = require('./handler');
var formidable = require('formidable');
//调用具体方法
var method = {};
method['/'] = handler.home;
method['/home'] = handler.home;
method['/upload'] = handler.upload;
//交给服务器模块处理
server.start(router.route, method);
2.5 首页中上传模块HTML文件 <待补充>
最早的HTTP协议中是不支持文件上传的,在1995年制定的rfc1867规范中,在HTTP POST请求的内容类型Content-Type
中扩展了multipart/form-data
类型,该类型用于向服务器发送二进制数据,以便支持文件的上传。
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
//上传文件时需要通过enctype
属性将编码方式设置为multipart/form-data
<form action="/upload", enctype="multipart/form-data" method="post">
<input type="file" name="upload" multiple="multipart">
<input type="submit" value="提888交">
</form>
<form action="/download", enctype="multipart/form-data" method="get">
<input type="submit" value='下载'>
</form>
</body>
</html>
知道你需要Demo,请查看原文链接。-_-
欢迎转载、点赞。
注意:本文归作者所有,未经作者允许,不得转载