Upload and redirect page using multer
<p>I am having a problem to redirect the page after a successful file upload using <code>multer</code>. With the file upload i am also saving some text into the database. Here's my code.</p>

<p>Question :</p>

<p>When the file and the contents are saved in the DB how can I redirect the page to a new URL ?</p>

<p>I am currently using <code>res.redirect('product/item');</code> but nothing happens. I also tried using <code>res.render</code>, but the page did not redirect.</p>

<p><strong><code>Multer</code> method to upload a file to <code>Amazon S3</code></strong></p>

<pre><code>var upload = multer({
    storage: multerS3({
    s3: s3,
    bucket: 'nameofthebucket',
        metadata: function (req, file, cb) {
            var ext = file.originalname.split('.').pop();

            cb(null, {fieldName: 'file.fieldname' + '.' + ext});
        },
        filename: function(req,file,cb){
            var ext = file.originalname.split('.').pop();
                           cb(null,   Date.now() + '.' + ext);
        },
            key: function (req, file, cb) {
                var ext = file.originalname.split('.').pop();
                cb(null,   Date.now() + '.' + ext);
        }
    })
})


var upload = upload.array('fileup', 10);
</code></pre>

<p><strong>The code responsible to upload the file and the content</strong></p>

<pre><code>router.post('/uploadfileandcontent',function(req,res,next){

    upload(req,res,function(err) {
        if(err) {
        } else {


            saveRecordsToDB(req, function(err,data){
                if (err) {

                    res.redirect('/errorpage');
                } else {
                    res. redirect('product/item');

                }

            });


        }

    });



});
</code></pre>

<p><strong>Function that saves records to DB and makes the callback</strong></p>

<pre><code>function saveRecordsToDB (req, callback){

            var args = {
                data: {
                        "name" : req.body.name, //
                        "age" : req.body.age

                },

                headers: { "Content-Type": "application/json" }

            };


            // registering remote methods 
            client.registerMethod("postMethod", "http://url/uploadfileandcontent", "POST");

            var req =client.methods.postMethod(args, function (data, response) {


                callback(null, 'success?');

            });

            req.on('error', function (err) {
                console.log('error');


            });
}
</code></pre>

<p>Note: I also made use of <a href="https://www.npmjs.com/package/node-rest-client" rel="nofollow">NODE REST CLIENT</a> to send http request.</p>