node进修系列之简朴文件上传

此为看完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

    原文作者:jasminecjc
    原文地址: https://segmentfault.com/a/1190000007253069
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞