资讯专栏INFORMATION COLUMN

node学习系列之简单文件上传

Achilles / 2728人阅读

摘要:此为看完入门写的小自己实现了一遍,借助的外部模块只有的模块的使用原代码仓库

此为看完node入门写的小demo,自己实现了一遍,借助的外部模块只有formidable
Node.js的Formidable模块的使用

index.js

const server = require("./server");
const router = require("./router.js");
const requestHandler = require("./requestHandler.js");

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

server.start(router.route, handle);

server.js

const http = require("http");
const url = require("url");

const start = (route, handle) => {
    http.createServer((request, response) => {
        let pathname = url.parse(request.url).pathname;
        let postData = "";
        console.log("request from" + pathname + "received");

        route(handle, pathname, response, request);

    }).listen(8888);
}

console.log("server has started");

module.exports = {
    start: start
}

router.js

const route = (handle, pathname, response, request) => {
    console.log("about to route a request for" + pathname);
    if(typeof handle[pathname] === "function") {
        handle[pathname](response, request);
    }else {
        console.log("no request handle found for " + pathname);
        response.writeHead(404, {"Conten-Type": "text/plain"});
        response.write("404 not found");
        response.end();
    }
}

module.exports = {
    route: route
}

requestHandler.js

const querystring = require("querystring");
const fs = require("fs");
const formidable = require("formidable");

const start = (response, request) => {
    console.log("request handle "start" was called");
    let body = `
                
                    
                    Document
                
                
                    
`; response.writeHead(200, {"Conten-Type" : "text/html"}); response.write(body); response.end(); } const upload = (response, request) => { console.log("request handle "upload" was called"); let form = new formidable.IncomingForm(); form.uploadDir = "./upload/tmp"; form.parse(request, (err, fields, files) => { console.log("parse done"); let body = ` Document `; fs.renameSync(files.upload.path, "/tmp/test.png") response.writeHead(200, {"Content6-Type" : "text/html"}); response.write(body); response.end(); }) } const show = (response) => { console.log("request handle "show" was called"); fs.readFile("/tmp/test.png", "binary", (err, file) => { if(err) { response.writeHead(500, {"Conten-Type" : "text/plain"}); response.write(err); response.end(); }else { response.writeHead(200, {"Conten-Type" : "image/png"}); response.write(file, "binary"); response.end(); } }) } module.exports = { start: start, upload: upload, show: show }
原代码仓库

learn-node/upload

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/91046.html

相关文章

  • Express系列multer

    摘要:目前觉得对我有用的是和。后者可以在本地调试页面,对于手机页面尤其有用。这次主要说一下,我并没有实现所有的功能,只是实现单图片上传这一个功能,其他的再摸索喽。目前就这样,下次弄出来了多图片上传我再接着更新。 这两天在看《nodejs权威指南》,这本书看了好久了,但是读的一直不细,这次才好好看了一遍。 收获还是蛮多的,主要在于wenpack使用的一些细节问题,有点茅塞顿悟的体验吧,另外在n...

    Null 评论0 收藏0
  • JavaScript精编干货

    摘要:老姚浅谈怎么学鉴于时不时,有同学私信问我老姚,下同怎么学前端的问题。撸码听歌,全局控制。 浅析用 js 解析 xml 的方法 由于项目上需要解析 xml,于是各种百度,然后自己总结了下各个主流浏览器解析 xml 的方法,只能是很浅显的知道他的用法,但是还没有深层次的研究。 装 X - 建立自己的斗图网站库 之前加过一个斗图群,看到很多经典的表情,然后就收藏到了 QQ, 迫于本屌丝开不起...

    Fourierr 评论0 收藏0
  • H5学习

    摘要:为此决定自研一个富文本编辑器。本文,主要介绍如何实现富文本编辑器,和解决一些不同浏览器和设备之间的。 对ES6Generator函数的理解 Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同。 JavaScript 设计模式 ② 巧用工厂模式和创建者模式 我为什么把他们两个放在一起讲?我觉得这两个设计模式有相似之处,有时候会一个设计模式不能满...

    aristark 评论0 收藏0
  • 前端每周清单第 41 期 : Node 与 Rust、OpenCV 的火花,网络安全二三事

    摘要:的网站仍然使用有漏洞库上周发布了开源社区安全现状报告,发现随着开源社区的日渐活跃,开源代码中包含的安全漏洞以及影响的范围也在不断扩大。与应用安全是流行的服务端框架,本文即是介绍如何使用以及其他的框架来增强应用的安全性。 showImg(https://segmentfault.com/img/remote/1460000012181337?w=1240&h=826); 前端每周清单专注...

    syoya 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<