问题描述
我正在使用 sphere 引擎编译器 API 构建一个 Online Code editor for Java。
我使用 Sphere Engine Compilers API 为 Express 创建了一个代理服务器。并使用此代理服务器在客户端(前端)获取数据(编译后的程序输出)。但是当多个用户同时向代理服务器发送 post 请求时,所有用户都从代理服务器得到相同的响应。
const express = require("express");
const cors = require("cors");
require('dotenv').config();
const fetch = require('node-fetch');
const Bluebird = require('bluebird');
fetch.Promise = Bluebird;
var accesstoken = process.env.ACCEsstOKEN ;
var endpoint = process.env.ENDPOINT;
var stream,submissionId;
const app = express();
const PORT=process.env.PORT;
app.use(express.json());
app.use(cors());
app.get('/',(req,res)=>{
console.log("Got a request");
res.send("Hello User!");
});
app.post('/api/compiler/java',res)=>{
fetch('https://' + endpoint + '/api/v4/submissions?access_token=' + accesstoken,{
method: 'POST',mode: "no-cors",headers: {
'Content-Type': 'application/json',},body: JSON.stringify(req.body),})
.then(response => response.json())
.then( data => {
console.log('Success:',data);
submissionId= data.id;
})
.catch((error) => {
console.error('Error:',error);
});
setTimeout(() => {
fetch('https://' + endpoint + '/api/v4/submissions/' + submissionId + '?access_token=' + accesstoken)
.then(res=>res.json())
.then(response => {
// console.log('Success:',response.result);
var out= response.result.streams;
if(out.output){stream='output'}
else if(out.cmpinfo){stream='cmpinfo'}
else if(out.error){stream='error'}
console.log(stream);
})
.catch((error) => {
console.error('Error:',error);
}).then(()=>{
fetch('https://' + endpoint + '/api/v4/submissions/' + submissionId + '/'+stream+ '?access_token=' + accesstoken)
.then(res=>res.text())
.then(response => {
console.log('Success:',response);
res.send(response);
})
.catch((error) => {
console.error('Error:',error);
}); })
},5000);
});
app.listen(process.env.PORT,()=>console.log(`server is listening on port:${PORT}`));
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)