S
S
Stanislav Nekrasov2018-07-19 18:06:56
Node.js
Stanislav Nekrasov, 2018-07-19 18:06:56

How to process a POST request without unnecessary require'ov?

I want to process POST request with NodeJS and http module, how to do it without extra modules?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abcdefgk, 2018-07-19
@Metalofon

O! I washed down the materiel.
this is the form

<form enctype="application/x-www-form-urlencoded" method="POST" action="/submit">
  <input type="text" name="name" value="Вася"><br>
  <input type="text" name="surname" value="Пупкин"><br>
  <input type="submit" value="Send">
</form>

this is how it is accepted
if(req.url == '/submit' && req.method == 'POST') {
    req.on('data', function(data){
      var s = decodeURIComponent(data.toString());
      console.log(s);
      
      var obj = {};
      var arr = s.split('&');
      arr.forEach( el => {
        var a = el.split('=')
        obj[a[0]] = a[1];
      });
      console.log(obj);
    });
    return res.end('POST');
  }

with this result
name=Вася&surname=Пупкин
{ name: 'Вася', surname: 'Пупкин' }

gotta drink...

A
Alexander Aksentiev, 2018-07-19
@Sanasol

https://habr.com/post/327440/
but it's like a wild crap, where does the fear of extra modules come from?
Is it better to write a bunch of noodle code that no one will understand than to take a ready-made package and arrange routing in a couple of lines?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question