topic description
when using koa-router to process url routing in nodeJS"s KOA framework, the front end cannot get the server return value
sources of topics and their own ideas
related codes
/ / Please paste the code text below (do not replace the code with pictures)
front-end JavaScript:
$.ajax({
type: "POST",
url: "http://127.0.0.1:8081/uploadimgs",
data:formData,
dataType: "json", //json
//FormData
cache:false, //true
processData:false, //datafalse;trueFormDataString
contentType:false, //httpfalse
success: function(msg){
console.log("msg: "+msg);
console.log("");
//ajaxbase64
//
var outputDir="C:/ajax/output/"+$port.toLowerCase();
console.log("outputDir: "+outputDir);
//
$.ajax({
type: "POST",
url:"http://127.0.0.1:8081/backImgs",
data:{
outdir:outputDir
},
dataType:"json",
//
success:function(msg){
console.log("msg: "+msg);
subAndJump(msg);
},
error:function(err){
console.log("...");
console.log(err);
}
});
},
error:function(){
console.log("ajax");
}
});
backend nodeJS app.js
router.post("/uploadimgs",async (ctx,next) => {
//
childProcessExit=false;
let form = new multiparty.Form({
uploadDir:"C:/ajax/input/",
keepExtensions:true,//
autoFields:true,
autoFiles:true
});
//JSON
let errsign={status:500,exception:null};
let datasign={status:200,recordset:null};
//loadimg()
function loadimg() {
let send_json={};
return new Promise((resolve,reject)=>{
form.parse(ctx.req,function(err,fields,files){
if(err){
// throw err;
console.log(err);//Error: write after end
send_json={
exception:"",
err:false
};
resolve(send_json);
// return send_json;
}else{
//
controlMessage.port=fields.port;
controlMessage.nameCH=fields.name_ch;
controlMessage.nameEN=fields.name_en;
send_json={
recordset:"",
err:true}
resolve(send_json);
let workProcess=exec("python C:/ajax/main.py " +controlMessage.nameCH+" "+controlMessage.nameEN+" "+controlMessage.port+" ",function(error,stdout,stderr){
if(stdout.length >1){
console.log("you offer args:"+stdout);
} else {
console.log("you don\"t offer args");
}
if(error) {
console.info("stderr : "+stderr);
}
});
workProcess.on("exit",(code)=>{
console.log(":"+code);
childProcessExit=true;//true
console.log("childProcessExit: "+childProcessExit);
});
}
});
//
form.on("file",(name,file)=>{
// console.log("fileName="+file.fieldName);
// console.log("originalFilename="+file.originalFilename);
// console.log("path="+file.path);
fs.rename(file.path,"C:/ajax/input/"+file.originalFilename,(err)=>{
if(err){
throw err;
}
})
});
});
}
await loadimg().then(sendjson=>{
// console.log("sendjson = "+sendjson);
if(sendjson.err===false){
errsign["exception"]=sendjson.exception;
ctx.body=errsign;
}else{
datasign["recordset"]=sendjson.recordset;
ctx.body=datasign;
}
});
});
//post
router.post("/backImgs",async (ctx,next)=>{
console.log("");
var num=0;
//
var timer=setInterval(async function(){
numPP;
console.log(""+num+"");
if(childProcessExit){
console.log(1);
clearInterval(timer);
await pictureTrans().then(picBase64=>{
ctx.body=picBase64;
});
}
//6s
if(num>6){
clearInterval(timer);
throw new Error(",...");
}
},1000);
function pictureTrans(){
return new Promise((resolve,reject)=>{
console.log("pictureTrans")
let dir=ctx.request.body.outdir;
console.log(`: ${dir}...`);
//
var filePath = path.resolve(dir);
//
fs.readdir(filePath,function(err,files){
if(err){
console.warn(err)
}else{
//base64
var picBase64={};
//
files.forEach(function(filename){
//
var filedir = path.join(filePath,filename);
//fs.Stats
fs.stat(filedir,function(eror,stats){
if(eror){
console.warn("stats");
}else{
var isFile = stats.isFile();//
var isDir = stats.isDirectory();//
if(isFile){
console.log(`: ${filedir}...`);
let data = fs.readFileSync(filedir);
data = new Buffer(data).toString("base64");
console.log("data: "+data);
console.log(`data: ${typeof data}`);
console.log("filename: "+filename);
picBase64[filename]=data;
console.log(`picBase64.${filename}: ${picBase64[filename]}`);
}
if(isDir){
fileDisplay(filedir);//
}
}
})
});
//base64
console.log("base64");
resolve(picBase64);
}
});
});
}
//console.log("dir:"+ctx.request.body.outdir);
//,childProcessExit
//true,base64
});
app.use(router.routes());
app.listen(8081);
what result do you expect? What is the error message actually seen?
I see that the / uploadimgs access from the front end to the background was successful, and the message msg, and access / backImags failed.
the following is the console print:
console:
network:
PS:app.js shows that the file is read normally and has been successfully converted to base64 bit encoding
online, etc., ask the boss to provide some solutions OTZ