首页 > 解决方案 > 为什么可变路由路径在 Express.js 中不起作用并给出错误?

问题描述

我有这个小的 Express.js 计算器,它应该根据操作类型传递变量路由,并且操作数也作为变量传递。但是,给出一个错误。

var express = require("express");

var PORT = process.env.PORT || 8080;

var app = express();

let operation; let operand1; let operand2;

app.get(`/:operation/:operand1/:operand2`, function(req, res) {

  var result;

  switch (operation) {

  case "add":
    result=operand1+operand2;
    break;
  case "subtract":
    result=operand1-operand2;
    break;
  case "multiply":
    result=operand1*operand2;
    break;
  case "divide":
    result=operand1/operand2;
    break;
  default:
    result="Sorry! The only valid operations are add, subtract, multiply, and divide.";
  }


  res.send(result.toString());

});


app.listen(PORT, function() {

  console.log("Server listening on: http://localhost:" + PORT);
});

Node.js 给出的错误是

Error: Cannot find module 'C:\Users\19513\Desktop\In-Class-Exercises\calculator.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:282:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

标签: javascriptnode.jsexpress

解决方案


推荐阅读