无法使用express从nodejs请求对象访问文件

时间:2014-12-10 02:12:07

标签: node.js file-upload express

节点v0.10.30 表达@ 4.7.2

我想阅读发布到REST API的文件,我无法使用以下代码执行此操作。 我是nodejs的新手。

我尝试使用connect-busboy和下面的代码,但它似乎覆盖了body-parser。 正文解析器似乎不支持多部分文件上载。

我确信我将快速4与旧式(版本)编码混合在一起,任何见解都会有所帮助。 在此先感谢您的帮助。

            var express = require('express');
            var App = function(){

            // Scope
            var self = this;

            self.ipaddr  = process.env.OPENSHIFT_NODEJS_IP|| '127.0.0.1';
            self.port    = parseInt(process.env.OPENSHIFT_NODEJS_PORT|| 3000) ;
            if (typeof self.ipaddr === "undefined") {
            console.warn('No OPENSHIFT_NODEJS_IP environment variable');
            };

            // Web app urls
            self.app  = express();

            //This uses the Connect frameworks body parser to parse the body of the post request
            var bodyParser = require('body-parser');
            var methodOverride = require('method-override');
            // parse application/x-www-form-urlencoded
            self.app.use(bodyParser.urlencoded({extended: true}));
            // parse application/json
            self.app.use(bodyParser.json());
            // override with POST having ?_method=DELETE
            self.app.use(methodOverride('_method'))


            // Web app logic
            self.routes = {};

            //upload image
            self.routes['uploadImage'] = function(req, res){
            console.log("upload");
            console.log(req.files);
            res.end('success');
            };
            //new
            self.app.post('/ws/upload', self.routes['uploadImage']);

            //public
            self.app.use(express.static(__dirname));

            // Logic to open a database connection. We are going to call this outside of app so it is available to all our functions inside.
            self.connectDb = function(callback){
            self.db.open(function(err, db){
            if(err){ throw err };
            if(err){ throw err };
            callback();
            });
            };

            //starting the nodejs server with express
            self.startServer = function(){
            self.app.listen(self.port, self.ipaddr, function(){
            console.log('%s: Node server started on %s:%d ...', Date(Date.now()), self.ipaddr, self.port);
            });
            }

            // Destructors
            self.terminator = function(sig) {
            if (typeof sig === "string") {
            console.log('%s: Received %s - terminating Node server ...', Date(Date.now()), sig);
            process.exit(1);
            };
            console.log('%s: Node server stopped.', Date(Date.now()) );
            };

            process.on('exit', function() { self.terminator(); });

            self.terminatorSetup = function(element, index, array) {
            process.on(element, function() { self.terminator(element); });
            };

            ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGPIPE', 'SIGTERM'].forEach(self.terminatorSetup);

            };

            //make a new express app
            var app = new App();

            //call the connectDb function and pass in the start server command
            app.connectDb(app.startServer);

2 个答案:

答案 0 :(得分:0)

body-parser不提供处理multipart/form-data请求的中间件。有multer确实提供了这个功能,并建立在busboy之上。还有formidable / multiparty

答案 1 :(得分:0)

Multer工作。万一有人在寻找同样的问题,下面是代码。

        var express = require('express');
        var multer = require('multer');

        var App = function(){

        // Scope
        var self = this;

        self.ipaddr  = process.env.OPENSHIFT_NODEJS_IP|| '127.0.0.1';
        self.port    = parseInt(process.env.OPENSHIFT_NODEJS_PORT|| 3000) ;
        if (typeof self.ipaddr === "undefined") {
        console.warn('No OPENSHIFT_NODEJS_IP environment variable');
        };

        // Web app urls
        self.app  = express();

        //This uses the Connect frameworks body parser to parse the body of the post request
        var bodyParser = require('body-parser');
        var methodOverride = require('method-override');
        // parse application/x-www-form-urlencoded
        self.app.use(bodyParser.urlencoded({extended: true}));
        // parse application/json
        self.app.use(bodyParser.json());
        // override with POST having ?_method=DELETE
        self.app.use(methodOverride('_method'));
        self.app.use(multer({ dest: './uploads/'}));


        // Web app logic
        self.routes = {};

        //upload image
        self.routes['uploadImage'] = function(req, res){
        console.log("upload");
        console.log(req.files);
        res.end('success');
        };
        //new
        self.app.post('/ws/upload', self.routes['uploadImage']);

        //public
        self.app.use(express.static(__dirname));

        // Logic to open a database connection. We are going to call this outside of app so it is available to all our functions inside.
        self.connectDb = function(callback){
        self.db.open(function(err, db){
        if(err){ throw err };
        if(err){ throw err };
        callback();
        });
        };

        //starting the nodejs server with express
        self.startServer = function(){
        self.app.listen(self.port, self.ipaddr, function(){
        console.log('%s: Node server started on %s:%d ...', Date(Date.now()), self.ipaddr, self.port);
        });
        }

        // Destructors
        self.terminator = function(sig) {
        if (typeof sig === "string") {
        console.log('%s: Received %s - terminating Node server ...', Date(Date.now()), sig);
        process.exit(1);
        };
        console.log('%s: Node server stopped.', Date(Date.now()) );
        };