为什么可变路由路径在Express.js中不起作用并发出错误?

时间:2019-04-23 02:03:53

标签: javascript node.js express

我有这个小的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)

0 个答案:

没有答案