URIError: URI malformed in NodeJS

0

Server-side code

const bodyParser = require("body-parser");
const express = require("express");
const app = express();
const port = 8080;

app.use(bodyParser.urlencoded({extended: false}));
app.use((req, res, next)=>{
    res.setHeader("Access-Control-Allow-Origin", "http://localhost");
    res.setHeader("Access-Control-Allow-Methods", "GET, POST");
    res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type");
    res.setHeader("Access-Control-Allow-Credentials", true);
    next();
});
app.post("/api", (req, res)=>{
    res.set("Content-Type", "application/json");
    res.set("Accept-Charset", "utf-8");
        const msgData = {
            getMsg : req.body.phrase,
            geMsgDUC : decodeURIComponent(req.body.phrase),
        };
        console.log(JSON.stringify(msgData));
        res.send(JSON.stringify(msgData.geMsgDUC));
});
app.listen(port, (err)=>{
    if (err){
        throw err;
    }
    console.log("Server started on http://localhost:"+port);
});

Client-Side Requisition

fetch("http://localhost:8080/api", {
    method: 'POST',
    headers: {'Content-Type':'application/x-www-form-urlencoded'}, 
    body: "phrase="+encodeURIComponent("~(+´g°i.v=+[ÿqéºqiyk'ïìù;7ú´=%dz")
}).then(function(response){
    return response.json();
  }).then(function(json){
    console.log(json); 
  });

I'm having a server-side return error:

URIError: URI malformed
at decodeURIComponent (<anonymous>).

NOTE: The string is encoded and decoded normally in the browser console.

    
asked by anonymous 06.03.2018 / 01:55

0 answers