M
M
Maxim Nine2017-03-21 14:46:53
JavaScript
Maxim Nine, 2017-03-21 14:46:53

How to make such a variable?

There is a function execution in the request processing on the server side, you need to make a variable that will store request.body.title. In code

var app = require("express")();
var bodyParser = require('body-parser');
var fs = require('fs');
var http = require("http").Server(app);
var multer = require("multer");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));

var all_audio;

app.get('/', function(req, res) {
    res.send('Вы не задали параметров<hr><small>Эта страница была сгенерирована автоматически сайтом kulonful.ru</small>');
})

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '../../../../../../var/www/sources/audio/');
    },
    filename: function (req, file, cb) {
        var all_audio_cut = all_audio.slice(0,-1), ids = 0;
        for(var a = 0; a<all_audio_cut.length; a++)
        {
            if(all_audio_cut[a] == '{') {ids = ids+1;}
        }
        all_audio_cut = all_audio_cut + ',{"id":"' + ids + '","title":"Запись названия временно недоступна"}]';// место 1
        var filenames = "mus" + ids + ".mp3";
        all_audio = all_audio_cut;
        fs.writeFile("audio.cfg", all_audio_cut);
        cb(null, filenames);
    },
    onFileUploadComplete: function (file) {
    res.sendStatus(200);
    }
})

var upload = multer({storage:storage})

app.post('/upload', upload.single('file'), function (req, res) {
    res.send('Аудиозапись была загружена на сервер'); // Место 2
})
places are marked with comments, in the second place there is a variable req, it is necessary that its value gets into the first place.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2017-03-21
@EreminD

forgive me my stupidity. Why can't it be like this?

var currentTitle;

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '../../../../../../var/www/sources/audio/');
    },
    filename: function (req, file, cb) {
        var all_audio_cut = all_audio.slice(0,-1), ids = 0;
        for(var a = 0; a<all_audio_cut.length; a++)
        {
            if(all_audio_cut[a] == '{') {ids = ids+1;}
        }
        all_audio_cut = all_audio_cut + ',{"id":"' + ids + '","title":' + currentTitle + '}]';// место 1
        var filenames = "mus" + ids + ".mp3";
        all_audio = all_audio_cut;
        fs.writeFile("audio.cfg", all_audio_cut);
        cb(null, filenames);
    },
    onFileUploadComplete: function (file) {
    res.sendStatus(200);
    }
})

var upload = multer({storage:storage})

app.post('/upload', upload.single('file'), function (req, res) {
    res.send('Аудиозапись была загружена на сервер'); // Место 2
    currentTitle = req.body.title;
})

D
Dmitry, 2017-03-21
@Cryden

For the correct logic for executing asynchronous functions, use Promise. Good article for newbies.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question