Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (19.9k points)

I'm trying to redirect using nodejs and expressjs, but when I click on button nothing happens only url changes. I'm using a form and within it has a button, this form has an action to "/failure"

const express = require("express")

const bodyparser = require("body-parser")

const request = require("request")

const app = express()

app.use(express.static("public"))

app.use(bodyparser.urlencoded({

  extended: true

}))

app.get("/", function (req, res) {

  res.sendFile(__dirname + "/signup.html")

})

app.post("/failure", function(req, res){

   res.redirect("/")

})

app.listen(3000, function () {

  console.log("Server is running on port 3000")

})  

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Failure</title>

<!-- Bootstrap core CSS -->

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"

integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

</head>

<body>

<div class="jumbotron jumbotron-fluid">

<div class="container">

<h1 class="display-4">Uh oh!</h1>

<p class="lead">There was a problem signip you up Please try again or contact the developer!.</p>

<form action="/failure" method="POST">

<button class="btn btn-lg btn-warning" type="submit" name="button">Try again</button>

</form>

</div>

</div>

</body>

</html>

1 Answer

0 votes
by (25.1k points)

Have you tried

res.redirect(307, '/test');

});

This will preserve the send method.

Browse Categories

...