Alexandre Daubricourt Alexandre Daubricourt - 2 months ago 16
Node.js Question

Node.js How to get POST query

I'm new to Node.js, and, i would like to know how to make data "transite" between html files for exemple :

I get submit.html with 2 text inputs (name and age) and a submit button, with target sub.html,

Let's say that an user submit name=Alex, age=20, How to get this data in my sub.html script(sub.js) to render these data in HTML tag ? Does i need to use socket.io or Ajax maybe ? there isn't some methods in node.js ?

Thanks for answering to these noob questions ^^

Answer

You need to use a web application framework like express, koa to handle requests.

Here is one approach to above question using express as web framework:

app.get('/', function(req, res) {
  // code to render submit.html
});

app.post('/', function(req, res) {
  // gets value of the submit.html through a ajax request
  // save it in database( such as sqlite, mysql)
});

app.get('/welcome', function(req, res) {
  // retrieve stored data from database
  // render 2nd html (which displays name and age) using a templating enigne like pug (previously called jade)
});
Comments