简体   繁体   English

Node Js Webserver 总是链接到同一个页面

[英]Node Js Webserver always linking to the same page

Im trying to build a Webpage with multiple HTML Pages.我试图用多个 HTML 页面构建一个网页。

With the following code I am able to link to the login page, but the localstores-page also links to Login.使用以下代码,我可以链接到登录页面,但 localstores-page 也链接到登录页面。

const express = require('express')
const app = express()
const port = 3000
var path = require("path");
var request = require('request');
const { get } = require('./routes/brand');

//Static files
app.use(express.static('public'))
app.use('/css', express.static(__dirname +'public/css'))
app.use('/img', express.static(__dirname +'public/img'))
app.use('/js', express.static(__dirname +'public/js'))


app.get('/',(req, res) => {
   res.sendFile(path.join(__dirname +'/views/brand.html'));
})

   app.route('/*').get(function(req, res)  {
        res.sendFile(path.join(__dirname +'/views/login.html'));
});

app.route('/*').get(function(req, res)  {
   res.sendFile(path.join(__dirname +'/views/localstores.html'));
});

// listen on port 3000

app.listen(port,() => console.info(`Listening on port ${port}`))

In the HTML-file, i have the seperate buttons linked to the seperate pages, like so :在 HTML 文件中,我将单独的按钮链接到单独的页面,如下所示:

<a href ="login.html">
    SIGN IN</a></p>
</div>

<a href ='localstores.html'>LocalStores</a>

I hope someone can help me with this.我希望有人能帮我解决这个问题。 I am very new to HTML and NodeJS.我对 HTML 和 NodeJS 很陌生。

app.route('/*').get will execute for all get requests, such as navigation. app.route('/*').get将针对所有获取请求执行,例如导航。 Since you have login.html first it gets returned.由于您首先拥有 login.html,因此它会返回。

You can delete those lines and update the href in your html file to be:您可以删除这些行并将 html 文件中的 href 更新为:

 <a href ='/views/localstores.html'>LocalStores</a>

Alternativley you can change it to:或者,您可以将其更改为:

  <a href ='/localstores'>LocalStores</a>

and add:并添加:

    app.route('/localstores').get(function(req, res)  {
        res.sendFile(path.join(__dirname +'/views/localstores.html'));
});

    app.route('/*').get(function(req, res)  {
        res.sendFile(path.join(__dirname +'/views/login.html'));
});

app.route('/*').get(function(req, res)  {
   res.sendFile(path.join(__dirname +'/views/localstores.html'));
});

these seem to be affecting the same route.这些似乎影响了同一条路线。 seems like express is just using the first one declared.似乎 express 只是使用声明的第一个。

My suggestion would be try this, so that each page has it's own individual route:我的建议是试试这个,这样每个页面都有自己的路线:

app.route('/login').get(function(req, res)  {
        res.sendFile(path.join(__dirname +'/views/login.html'));
});

app.route('/localstores').get(function(req, res)  {
   res.sendFile(path.join(__dirname +'/views/localstores.html'));
});

when you do:当你这样做时:

     app.route('/*').get(function(req, res)  {
     res.sendFile(path.join(__dirname +'/views/login.html'));
                                                       }); 

its basically saying to Node every Client/user that accesses any route after the root / run this function which returns the login.html page thats what /* means.它基本上是对节点说,每个客户端/用户在 root 之后访问任何路由/运行此函数,该函数返回login.html页面,这就是/*意思。

instead try specifying different routes to both pages like this:而是尝试为两个页面指定不同的路由,如下所示:

   app.get('/login',function(req, res)  {
    res.sendFile(path.join(__dirname +'/views/login.html'));
  });

  app.get('/localstores',function(req, res)  {
  res.sendFile(path.join(__dirname +'/views/localstores.html'));
  });

and in your HTML instead of putting the file names you should just use the Routes you defined and Express will handle sending the appropriate file to the client并且在您的 HTML 中而不是放置文件名,您应该只使用您定义的路由,Express 将处理向客户端发送适当的文件

  <a href ="login">
  SIGN IN</a></p>
  </div>

  <a href ="localstores">LocalStores</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM