javascript - 为什么可变路由路径在 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)
解决方案
推荐阅读
- flutter - Flutter:如何制作超过一屏的Form
- ember.js - 尝试运行 ./watch 时出现以下错误
- python - 比较 Pandas 数据框中不同行的两个值
- java - 将 txt 文件中的二维数组转换为 JavaFx(数独)
- r - 加权倾向得分 R 后标准误差的自举时间到事件模型
- reactjs - 如何通过数据(json)或模拟数据在prime react datatable CELL中传递事件?
- java - 使用休眠更新不同应用程序上的同一个表
- react-native - 在 react native 中使用 manage-wallpaper 库时出现错误
- php - FPM 中的 PDO 与 UTF8 的 CLI 的不同结果
- c - 如何从 C 中的同一地址读取不同的输出?