Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

271
Vistas
How can a JS Client pass POST parameters to a JS Server?

I'd like to have a pair of Javascript client and server that communicate using JSON without involving a Web Browser. I can get it going as long as the client does not try to send anything to the server. When it does, I get the error message (% node poster.js):

.../node_modules/xhr2/lib/xhr2.js:281
            throw new NetworkError(`Unsupported protocol ${this._url.protocol}`);

Can somebody help me with this protocol issue please. What I have so far: a server that I can start with "pm2 start jsonServer.js" -

var port = 62022
var http = require('http')
var srvr = http.createServer (function (req, res) {
        console.log ('Request type: ' + typeof(req))
        console.log (req)
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.write('Hello World!\n');
        res.write(req);
        res.end();
})
var scriptName = __filename.split(__dirname+"/").pop();
console.log ('Script: ' + scriptName +'. Listening to port ' + port)
srvr.listen(port)

... and a client that is

var XMLHttpRequest = require('xhr2')

var serialize = function(object) {
  return JSON.stringify(object, null, 2)
}

let xhr = new XMLHttpRequest();
xhr.open("POST", "localhost:62022");

xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");

xhr.onload = () => console.log(xhr.responseText);

let data = `{
  "Id": 912,
  "First Name": "Archibald",
  "Last Name": "Haddock"
}`;
//console.log (serialize(data));
//xhr.send(serialize(data));
console.log (data);
xhr.send(data);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This is what is needed after posting 'Hello World!' on the server (res.end does not wait for the async data transfer):

        let data = '';
        req.on('data', chunk => {
                data += chunk;
        });
        req.on('end', () => {
                // do whatever with data
                res.write ('Data: ' + data + '\n')
                res.end()
        });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda