首页 > 解决方案 > 如何在 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);

标签: javascriptnode.jsexpressthree.jsmulter

解决方案


推荐阅读