找不到带有参数404的路由

时间:2019-06-15 12:22:49

标签: node.js express pug

我将需要一些帮助,因为在此阶段之后我迷路了,我正在尝试使用参数进行路由,但是此后我迷路了,当页面路由显示时,如何将页面加载到此url localhost:3000 / dist / 16783找不到404。我需要一些帮助,如何才能在ID为“ 16783”的表之后使用表ex中的值导航到新页面:不跟踪“ 16783”或在同一页面上加载视图。

router.get('/dist/:awb', function (req, res, next) {
    res.render('dist', {
        AWB_NO: req.params.awb
    })
});

它不呈现页面“ dist”,这个问题可以与此相关 this documentation

编辑: 结构:

views:
   index.ejs
   track.ejs
   dist.ejs
routes:
   index.js
   track.js
   dist.js
app.js
package.json

app.js:

var indexRouter = require('./routes/index');
var trackRouter = require('./routes/track');
var distRouter = require('./routes/dist');

app.use('/', indexRouter);
app.use('/track', trackRouter);
app.use('/dist',distRouter);

track.ejs

表格列AWB否链接到dist页面

 a.name(href="/dist/" + AWB_NO) #{AWB_NO}
            div
              td.min(data-label="Name") #{Product_Name}
              td.min(data-label="file") #{PCS}
              td.min(data-label="s_name") #{Weight}
              td.min(data-label="r_name") #{Gross_Weight}
              td.min(data-label="Date") #{DOP}
              td.min(data-label="Date") #{DOE}

所以这是实际的问题,因为当我在链接到“ dist”页面的“ AWBNO”列中具有值时。现在,当我将其传递的参数单击到url dist / 16783时,却没有加载任何内容

路线:

track.js

router.get('/', function(req, res, next) {
res.send('respond with a resource');

dist.js

var express = require('express');
var router = express.Router();
router.get('/', function (req, res, next) {
    res.render('dist.ejs');
});

router.get('/:awb', function (req, res, next) {
    res.render('dist.ejs', {
        AWB_NO: req.params.awb
    })
});

新更新:

index.ejs

  <tr>
        <td class="id"><a href="/dist">1687952</a></td>
        <td>Tuna Fish</td>
        <td>DHL</td>
        <td>Germany</td>
      </tr>

index.js

router.get('/', function (req, res, next) {

    connection.query('SELECT * FROM orders', function (err, rows) {

        if (err) {
            req.flash('error', err);
            res.render('index', { page_title: "index - Node.js", data: '' });
        } else {

            res.render('index', { page_title: "index - Node.js", data: rows });

        }


    });
});

dist.ejs

<li class="list-inline-item">AWB Nummber:<%= AWB_NO %></li> &nbsp;

dist.js

router.get('/', function (req, res, next) {

    connection.query('SELECT * FROM deliveries', function (err, rows) {

        if (err) {
            req.flash('error', err);
            res.render('dist', { page_title: "dist - Node.js", data: '' });
        } else {

            res.render('dist', { page_title: "dist - Node.js", data: rows });

        }


    });
    router.get('/dist/:awb', function (req, res) {
        res.render('dist', {
            AWB_NO: req.params.awb
        })
    });
});

错误:

ReferenceError: E:\Dev\admeghbalim\YinSeafood\YinSeafood\expressfirst\views\dist.ejs:20
    18|             <h3>Distributor information</h3><br>&nbsp;
    19|             <ul class="list-inline">
 >> 20|                 <li class="list-inline-item">AWB Nummber:<%= AWB_NO %></li> &nbsp;
    21|                 <li class="list-inline-item">Country:</li>&nbsp;
    22|                 <li class="list-inline-item">Date:</li>&nbsp;
    23|                 <li class="list-inline-item">Sender:</li>&nbsp;

AWB_NO is not defined

1 个答案:

答案 0 :(得分:0)

我认为此处的路由可能与您在快速基本配置中设置的路由有关。

如果您的app.js中有类似的内容

var express = require("express");

var app = express();
app.use('/dist', require("./routes/dist"));
//Set the view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

您的dist.js有一条路线

router.get('/dist/:awb', function (req, res, next) {
    res.render('dist', {
        AWB_NO: req.params.awb
    })
});

您的路线将有效

  

localhost:3000 / dist / dist / 16783

,因为该路径相对于app.use中的根路径。在这种情况下,您的路径应该是

router.get('/', function (req, res, next) {
    res.render('dist');
});

router.get('/:awb', function (req, res, next) {
    res.render('dist', {
        AWB_NO: req.params.awb
    })
});