Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用html按钮在node express中正确重定向?_Javascript_Node.js_Express - Fatal编程技术网

Javascript 如何使用html按钮在node express中正确重定向?

Javascript 如何使用html按钮在node express中正确重定向?,javascript,node.js,express,Javascript,Node.js,Express,我正在尝试通过express render将我的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" )

我正在尝试通过express render将我的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');
   });
<button onclick="location.href='other'">
home.ejs中的代码如下所示:

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');
   });
<button onclick="location.href='other'">

但当我运行站点并单击按钮时,它会将我重定向到
http://localhost:3000/home/other
然而,我需要它将我重定向到
http://localhost:3000/other


我知道我犯了一些新手错误,但我现在似乎无法指出,非常感谢您的帮助

注意
上面给出的代码简化了我的问题,因为编写整个代码非常困难。因此,如果我在解释中犯了任何错误,那么请忽略它们,试着理解Q的本质,试着:

<button onclick="window.location = 'http://stackoverflow.com'">


通过添加斜杠来使用相对于根目录的URL:
@blex哦,我完全忘记了,非常感谢您,如果您能将其作为答案发布,我会将其标记为正确:)