首页 > 解决方案 > 如何接受来自 Node.js web application.js 的 POST 请求并返回响应?

问题描述

看不懂是什么问题,这个请求的本质是传递一些数据,调用一个函数,然后返回生成的Json文件,但是这并没有发生,而是服务器什么都没有返回。可能是什么问题呢?请帮我

Web 部件

clickOnRow: function(elem, whichScreen){
          this.clicks++          
          
          if(this.clicks === 1) {
            var self = this
            this.timer = setTimeout(function() {            
            console.log("одинарный");                 
              self.clicks = 0
            }, this.delay);
          } else{
             clearTimeout(this.timer);
             console.log("двойной");
             
             elem['whichScreen']  = whichScreen;    
             console.log(this.helper);
            //  this.nameOfMethod(elem);
             this.clicks = 0; 
            fetch('/currentDir1',{
                    method: 'POST',
                    mode: 'cors',
                    headers: {
                        'Content-Type': 'application/json',                    
                    },
                    body: JSON.stringify(elem)                
                    })
                    .then(response => response.json())    
                    .then(json => this.helper = json)
                    .then(json =>  this.$emit("newvalue", json)) 
                    console.log("helper");
                    console.log(this.helper);

服务器部分

router.post('/currentDir1',(req, req) =>{  
    console.log("POST");
    
    let body = "";   
    let pathToFile = "";
    req.on("data", function (data) {
        body += data;
    });
    
    req.on("end", function(currentData) {
        console.log(JSON.parse(body));
        currentData = JSON.parse(body);
        

        if(currentData.sizeOrType === "<папка>"){
            let dir = currentData.dir + currentData.fileName;
            // dir = "C:\\totalcmd";
            console.log(dir);                
            if(currentData.whichScreen){
                foo(dir, './data/firstScreen.json');
                pathToFile = './data/firstScreen.json';
                res.sendFile(path.resolve('./data/firstScreen.json'));
            }else{
                console.log('aaaa');
                Foo(dir, './data/secondScreen.json');
                pathToFile = './data/firstScreen.json';
                res.sendFile(path.resolve('./data/secondScreen.json'));
                
            }        
        }
    
        // res.json({ message: 'goodbye'})   
        res.json(path.resolve(pathToFile));     
    });
       
    res.sendFile(path.resolve(pathToFile));
})

标签: javascriptnode.jsvue.js

解决方案


推荐阅读