3

I'm creating a landing page that when hit, automatically triggers a Facebook authentication flow. However, I'm unable to automatically redirect from the index to the signup route. How do I redirect from a GET request to a POST request in express?

    router.get('/', function(req, res) {
        //redirect to login function
    });

    router.post('/signup', passport.authenticate('signup', {
      //Log user in on facebook
    }));
Patrick Connors
  • 5,677
  • 6
  • 27
  • 54

2 Answers2

6

It's impossible to redirect from a GET request to a POST request.

All you can do is to redirect a POST request to a POST request but even that can be sometimes tricky, but you will never be able to redirect to a different method than the original request (unless that different method is GET).

Usually you do redirects by sending either 301 Moved Permanently or 302 Found HTTP status code. Both of them usually work in practice as if it was 303 See Other and a GET request is made.

There is a 307 Temporary Redirect (since HTTP/1.1) that is not allowed to change the HTTP method. That can be used to redirect a POST to a POST, but note that it is explicitly not allowed to change the method so you cannot use it to redirect a GET to a POST (or even a POST to a GET, which is otherwise possible with a simple 301 or 302 response).

See those answers for some more background:

rsp
  • 107,747
  • 29
  • 201
  • 177
  • That's unfortunate. I'll look into those links - thanks. – Patrick Connors Jul 18 '17 at 18:56
  • @PatrickConnors you can still do what you want. Rsp is just talking about true redirects. Or more your not explaining it well and rsp is misunderstanding what you mean. I get what you want to do and it's not hard. – Darkrum Jul 18 '17 at 19:00
  • 2
    @Darkrum Cool, how? – Patrick Connors Jul 18 '17 at 19:03
  • @PatrickConnors on your landing your checking if the user still has a good session correct and or login? If not send them to signup where your using Facebooks oauth api. – Darkrum Jul 18 '17 at 19:05
  • Just search for how to use passport to make a multi login page that should point you in the right direction – Darkrum Jul 18 '17 at 19:12
0
const express= require('express');
const cors=require("cors")
const mongoose=require('mongoose');
const bodyParser=require('body-parser');
const dotenv=require('dotenv');
dotenv.config();

DB=process.env.DB_url;
mongoose.connect(DB,{useNewUrlparser:true});`enter code here`

const ProductsSchema= new mongoose.Schema({
       productname:{
       type:String,
       require:true
       },
       price:{
      type:String  
      },
       category:{
       type:String
   },
image:{
    type:String
}

})

 const Products= mongoose.model("product",ProductsSchema);
const app=express();
app.use(cors());
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false}))

app.get("/product",async (req,res)=>{
     try {
    const products = await Products.find({});
     res.send(products)
    } catch(err) {
      res.status(500).json({message:err}) 
   }
 })

  app.post("product",(req,res)=>{
   try {
    let product={
        productname:req.body.productname,
        price:req.body.price,
        category:req.body.category,
        image:req.body.image
    }
    product.save();
    res.send(product)
} catch (err) {
    res.status(500).json({message:err})
}

})

  app.delete("/product/:id", async (req,res)=>{
try {
    const productId=req.params.id
const product1= Products.findByIdAndDelete(product)
res.send(product1)
    
} catch (err) {
    res.status(404).json({message:"not exist"})
}

})

  const PORT=process.env.PORT
 app.listen(PORT,()=>{
    console.log(`Server is up on PORT:${PORT}`);
 })
elis
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 17 '23 at 18:11