@Galdar
Web, JS, PHP, NGINX, Linux

Как принять на сервере node js from-data?

Вот я на сервер передаю from-data
postman

5f8dbb3654e89365864113.png


Но на сервере пустой объект. Кто подскажет что исправить чтобы видеть ответ?
script

class methods {

    constructor( manifest, loggers, app, bodyParser, auth, methodExe, aliases ) {

        this.app                = app
        this.auth               = auth
        this.methodExe          = methodExe
        this.aliases            = aliases

        this.bodyParser         = bodyParser

        this.manifest           = manifest
        this.PORT               = this.manifest.connection.PORT
        this.HOST               = this.manifest.connection.HOST
        this.loggers            = loggers

        this.PATH_API       = `/${this.manifest.version.PATH}/`

        this.app.use(this.bodyParser.json())
        this.app.use(this.bodyParser.urlencoded({ extended: false }))
     
    }

    /**
     * Инициализация методов
     * @method init
     */
    init() {

        this.app
        // Путь
        .route('*')
        // POST
        .post((req, res) => {

            console.log(req.body)

            let objectAction = {}

            let path = req.path.split('/')

            path = path.filter(element => element !== null && element !== undefined && element !== '')

            if( path[1].toUpperCase() == 'FILEREQ' )
            {
                objectAction.FILEREQ    = path[1]
                objectAction.method     = path[1]
                objectAction.action     = path[2].toUpperCase()
            }
            else
            {
                objectAction.db         = path[1]
                objectAction.method     = path[2].toUpperCase()
            }

            objectAction.auth       = req.body
            
            console.log(objectAction)
            this.loggers.info( `Connect: ${JSON.stringify(objectAction)}` )

            return new Promise( resolve => {

                this.auth.init( objectAction )
                .then( response => {

                    if( response.status == 403 || response.status == 501 || response.status == 401 || response.status == 404 )
                    {
                        const { status, send } = response
    
                        this.loggers.error( `User: ${objectAction.auth.user}\nStatus: ${response.status}\nText: ${JSON.stringify( response.send )}` )

                        return res.status(status).send( send )
                    }
    
                    resolve( response )
                    
                }) 

            })

            .then( response => {

                return new Promise( (resolve, reject) => {

                    resolve( response )

                })

            })

            .then( response => {
                
                return new Promise( resolve => {

                    this.methodExe.init( response )
                    .then( result => {

                        resolve( result )

                    })

                })

            })

            .then( response => {

                return res.status(200).send( response )

            })

        })

        this.app.listen(this.PORT, this.HOST, () => {
            this.loggers.info(`Server listens http://${this.HOST}:${this.PORT}`)
            console.log(`Server listens http://${this.HOST}:${this.PORT}`)
        })

    }

}

module.exports = methods

  • Вопрос задан
  • 33 просмотра
Пригласить эксперта
Ответы на вопрос 1
@AzizbekAzizbek
А вы сделали?:
JSON.parse(req.toString)
Нуу ведь данные получаются в виде Buffer
Или другой вариант: req.on('data', callback) отсутствует и сервер не дождался полного получения данных
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы