javascript - 如何在 Node.js 中调用 app.post 中的函数?
问题描述
我正在尝试创建一个节点应用程序,我需要在其中计算用户上传的 stl 文件的体积。我正在使用 express 和 multer 来处理文件上传过程。我正在使用three.js 来计算stl 文件的体积。但是,当我在 app.get() 中添加音量代码时,它不起作用。我已经在纯 js 上尝试了代码,它运行良好并输出音量。我不确定代码是否在节点上运行时被执行,因为终端控制台中没有打印任何内容。我需要这段代码在服务器端工作,因为我不希望客户端看到这段代码并对其进行操作。请帮我解决一下这个。这是我当前的代码:
app.get('/', function (req, res) {
console.log("Hello!"); //This line gets printed
res.render('pages/index');
});
app.post('/upload', upload.single('stl_file'), (req, res) => { //stl_file is the name attribute of input tag in html
var loader = new STLLoader();
loader.load(req.file, function (geometry) {
**Code to calculate volume**
console.log("stl volume is " + volume); //This line does not get printed
**Code to calculate size**
let stlSize = size;
console.log(stlSize); //This line does not get printed
});
return res.json({ status: 'OK'});
});
app.listen(3001);
解决方案
推荐阅读
- bash - 为什么这个while循环在第一个循环之后不执行?
- ios - Nativescript-vue ios gridlayout 进入状态栏并在点击动作后向下移动
- javascript - 数字到等级字母
- google-maps - 如何修复 flutter_google_places_autocomplete.dart 的错误?
- php - 如何为 API Wrapper 和外部 API 编写 PHP 单元测试,方法是检查 Class 发出的请求是否符合模式
- python - 几个大嵌套循环的小循环与小嵌套循环的大循环性能?
- ios - 是否可以将 ios 13 beta 安装到 ios Simulator 中?
- javascript - 如何在 Dynamic 365 CRM 客户端 Javascript 中获取用户属性?
- angular - 使用 ngx-extended-pdf-viewer 访问多个 pdf
- javascript - 如何使用 Array.prototype.map 和没有 Array.prototype.filter() 在 javascript 中映射没有未定义值的项目