Home > database >  LongUrl in Node req.params
LongUrl in Node req.params

Time:01-08

i would like to ask, how i can send parameter / value with longUrl from React front-end to Node back-end.

This code not work. Have somebody with this practice - if it is possible?

this is React front-end

const ulozeniZmen = (event) => {
        fetch(
            'https://serverWithNodeApp.com/insert/'   id   '/'   name   '/'   longurl, { method: 'POST'}
        )
        event.preventDefault();
    }

this is Node back-end

app.post( '/insert/:id/:name/:longurl', function ( req, res ) {
                var post  = {
                    id: req.params.id,
                    name: req.params.name,
                    longUrl: req.params.longurl,
                };
                connection.query( "INSERT INTO `test` SET ?", post, ( error, rows ) => {
                    if( error ) throw error;
                    if( !error ) {
                    res.status( 200 ).send(rows);
                    }
                } )
            } );

CodePudding user response:

I will try this front end:

encodeURIComponent(longUrl) 

and back end:

decodeURIComponent(longUrl)

CodePudding user response:

I resolved my problem. Url params i was changed to Json object

New version code front-end:

const ulozeniZmen = (event) => {
   fetch('https://serverWithNodeApp.com/insert/',{
       method: 'POST',
       headers: { 'Content-Type': 'application/json' },
       body: JSON.stringify({
          id: id, name: name, longurl: longurl
       })
   })
   event.preventDefault();
}

New version code back-end:

app.post( '/stream/test/', function ( req, res ) {
    let data = {id: req.body.id, name: req.body.name, longUrl: req.body.longurl};
    connection.query( "INSERT INTO `stream` SET ?", data, ( error ) => {
        if( error ) throw error;
        if( !error ) {
            res.status( 200 ).send(rows);
        }
    })
});
  • Related