![](/img/trans.png)
[英]How to send a JSON response and redirect to some html page after success in Node.js using express?
[英]How to redirect properly in node express using html button?
我正在嘗試通過快速渲染將我的 html 頁面鏈接到其他頁面
我的目錄看起來像這樣
app.js
/views
|---/images
|---home.ejs
|---other.ejs
app.js 中的代碼如下所示:
const express= require('express');
const ejs = require('ejs');
const app=express();
app.use( express.static( "views" ) );
app.set('view engine','ejs')
app.listen(3000,()=>console.log("listening at Port 3000..."));
app.get('/home',(req,res)=>{
res.render('./home.ejs');
});
app.get('/other',(req,res)=>{
res.render('./other.ejs');
});
app.get('/',(req,res)=>{
res.redirect('/home');
});
home.ejs 中的代碼如下所示:
<button onclick="location.href='other'">
但是當我運行該站點並單擊按鈕時,它會將我重定向到http://localhost:3000/home/other
而我需要它將我重定向到http://localhost:3000/other
我知道我犯了一些菜鳥錯誤,但目前我似乎無法指出,非常感謝任何幫助
筆記
上面給出的代碼是對我的問題的簡化,因為編寫整個代碼非常困難。 所以如果我在解釋中犯了任何錯誤,請忽略它們並嘗試理解 Q 的本質
嘗試:
<button onclick="window.location = 'http://stackoverflow.com'">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.