node/express锚点link路线

node/express anchor link route

我正在制作一个网站,对于前端,我正在使用 HTML。对于后端,我使用 node/Express。

在 HTML 中,我有一些锚标记可以重定向不同的 HTML 页面。为了创建不同的路由,我在 Express 中使用了 Router。

现在的问题是,当我 运行 我的网站通过节点时,重定向没有按预期工作。

index.html:

        <li><a href="index.html">Home</a></li>
        <li><a href="publication.html">Publication</a></li>
        <li><a href="team.htm">Team</a></li>
        <li><a href="contact.html">Contact</a></li>

ExpressJS:

index.js:

  var express = require('express');
  var path = require('path');
  //router object
 var router = express.Router();

 router.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, '..', '/public/html/index.html'));
 });

  module.exports = router;

publication.js:

   var express = require('express');
   var path = require('path');

  //router object
  var router = express.Router();

  router.get('/publication', function(req, res){
    res.sendFile(path.join(__dirname, '..', '/public/html/publication.html'));
  });

  module.exports = router;

当我通过节点运行网站并输入:

localhost:3000/publication

然后我得到 publication.html 并且当我输入

localhost:3000

它打开 index.html 并且在我的索引页面中,我有上面的锚标记。 现在,如果我单击我的任何锚标记,则会收到一条错误消息,如 "cannot get publication.html" 我想要的是重定向到我的 publication.js 并调用

router.get('/publication')

方法然后打开publication.html

谁能给我指出正确的方向?

这个代码

    <li><a href="index.html">Home</a></li>
    <li><a href="publication.html">Publication</a></li>
    <li><a href="team.htm">Team</a></li>
    <li><a href="contact.html">Contact</a></li>

将生成类似 http://localhost:3000/index.html 的链接。现在你的后端不知道这条路线,因为你定义了你的路线,比如 /publication 等等。您必须将其修改为如下所示

    <li><a href="/">Home</a></li>
    <li><a href="publication">Publication</a></li>
    <li><a href="team">Team</a></li>
    <li><a href="contact">Contact</a></li>

更多关于express routing