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

jasminecjc

此为看完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 = `<html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <title>Document</title>
                </head>
                <body>
                    <form action="/upload" method="post" enctype="multipart/form-data">
                        <input type="file" name="upload">
                        <input type="submit" value="submit">
                    </form>
                </body>
                </html>`;
    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 = `<html lang="en">
                    <head>
                        <meta charset="UTF-8">
                        <title>Document</title>
                    </head>
                    <body>
                    <img src="/show" />
                    </body>
                    </html>`;
        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

阅读 1.2k

jascheng的前端学习
前端学习路上的笔记和总结
952 声望
37 粉丝
0 条评论
952 声望
37 粉丝
宣传栏