如果我想通过server.get(“ / [url] /:params”)获得server.get(“ / [url] /”),我该怎么办?还是有可能..?

时间:2019-12-05 06:45:44

标签: node.js express

从字面上看,我想通过server.get(“ / [url] /:params”)获得server.get(“ / [url] /”)。

因为我不想调用以下两个函数。

Sub Button2_Click()
    Dim wb As Workbook, bk As Workbook
    Dim sh As Worksheet

    Set wb = ThisWorkbook

    For Each sh In Sheets
        With sh
            Set bk = Workbooks.Add(xlWBATWorksheet)
            .Range("E2:G15").Copy bk.Sheets(1).Range("A1")
            bk.SaveAs "C:\Users\Dave\AppData\Local\Temp\" & sh.Name & ".xlsx"
            bk.Close
        End With
    Next sh

End Sub

那我该怎么办..?

const server = express();

    server.get("/products/:id", (req, res) => {
        const actualPage = "/products";
        const queryParams = { id: req.params.id };
        app.render(req, res, actualPage, queryParams);
    });
    server.get("/products/", (req, res) => {
        const actualPage = "/products";
        const queryParams = { id: req.params.id };
        app.render(req, res, actualPage, queryParams);
    });

谢谢您的阅读

1 个答案:

答案 0 :(得分:0)

如果我是你,我会像lhis那样做:

const server = express();

function handleProductRequest(req, res) {
  const actualPage = "/industries";
  const queryParams = { id: req.params.id };
  app.render(req, res, actualPage, queryParams);
}

server.get("/products/:id", handleProductRequest);
server.get("/products/", handleProductRequest);

由于这些动作在重复,所以为什么不创建一个函数并在需要时调用它。