Skip to content
Advertisement

Post a form in Node Js

I am trying to post data from a form in node JS and then see the data in console. When i click the button I can see the data in console but the websites starts loading and it doesnt end. The loading circle doesnt stop moving.

<form method="post" action="/">
    <input type="text" name="user[name]">
    <input type="submit" value="Submit">
</form>

and this is in my server.js file

app.use(express.urlencoded({ extended: true }));
// Parse JSON bodies (as sent by API clients)
app.use(express.json());

// Access the parse results as request.body
app.post('/', function(request, response){
    console.log(request.body.user.name);
});

Advertisement

Answer

Your backend has to send a response, e.g. response.sendStatus(200), otherwise the frontend will wait until it timeouts.

Example:

app.use(express.urlencoded({ extended: true }));
// Parse JSON bodies (as sent by API clients)
app.use(express.json());

// Access the parse results as request.body
app.post('/', function(request, response){
    console.log(request.body.user.name);
    response.sendStatus(200);
});
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement