无法使用 Express 和 EJS 从 POST 请求重定向

Unable to redirect from POST Request with Express and EJS

我正在用 Node.js、Express 和 EJS 制作一个应用程序,它应该将您重定向到不同的页面(例如,从 localhost:3000/ 到 localhost:3000/about)按下一个按钮。 post 请求正在被服务器接收,因为 'post request' 被记录到服务器端控制台,但我没有被重定向,没有错误消息或任何东西。

服务器:

const express = require('express')
const app = express()
app.listen(3000)
app.set('view engine', 'ejs')
app.use(express.static('public'))

app.get('/', (req, res) => { //works as intended
    res.render('home')
})

app.post('/', (req, res) => { 
   res.redirect(307, 'https://google.com') //nothing happens, google.com as an example
   console.log('post request') //logged on server console
})

客户:

button = document.getElementById('button')
text = document.querySelector('textarea')
button.addEventListener('click', () => {
    fetch('/', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
            data: text.value
        })
    })
})

您在客户端应用程序中调用的 fetch() 函数是一个可让您从 node.js API 获取信息的函数。但是,它不允许您重定向到另一个页面。

如果你想被重定向,你可以使用 html <a> 元素指向你的 Express 服务器的 URL,在那里你可以被重定向到另一个页面。

服务器代码:

app.get('/redirect-me', (req, res) => {
   res.redirect(307, 'https://google.com') //nothing happens, google.com as an example
   res.render('home')
})

客户代码(html):

<a href="/redirect-me">Click here and you will be redirected to Google</a>