Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可否支持CORS #5

Closed
aston314 opened this issue Sep 2, 2024 · 3 comments · Fixed by #10
Closed

可否支持CORS #5

aston314 opened this issue Sep 2, 2024 · 3 comments · Fixed by #10
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@aston314
Copy link

aston314 commented Sep 2, 2024

Access to XMLHttpRequest at 'http://xxxxx.com/translate' from origin 'null' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

@guobao2333 guobao2333 added enhancement New feature or request help wanted Extra attention is needed labels Sep 2, 2024
@guobao2333 guobao2333 self-assigned this Sep 2, 2024
@guobao2333 guobao2333 added this to the 跨域访问和认证 milestone Sep 2, 2024
@guobao2333
Copy link
Owner

这个问题不是很难解决,但这几天我暂时没有时间去处理,如果有人能够为此贡献那是最好的,如果没有的话,我会在之后的闲暇时间解决CORS,大概在一周内吧。

@aston314
Copy link
Author

aston314 commented Sep 3, 2024

import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors'; // 引入 cors 模块
import { translate } from './translate.js';

const app = express();
const PORT = 9000;
const allowAlternative = true;

// 使用 CORS 中间件
app.use(cors());

app.use(bodyParser.json());

// 为了方便兼容多平台才这样写
app.post('/translate', async (req, res) => await post(req, res));
app.get('/', async (req, res) => await get(req, res));

async function post(req, res) {
const startTime = Date.now(); // 记录开始时间

// 检查请求方法和请求体
if (req.method !== 'POST' || !req.body || !req.body.text) {
const duration = Date.now() - startTime;
console.log([LOG] ${new Date().toISOString()} | 404 | ${duration}ms | POST "translate");
return res.status(404).json({
"code": 404,
"message": "Path not found"
});
}

// 判断是否备选翻译
if (!allowAlternative) alt_count = 0;
const { text, source_lang, target_lang, alt_count } = req.body;

try {
const result = await translate(text, source_lang, target_lang, alt_count);
const duration = Date.now() - startTime; // 计算处理时间
console.log([LOG] ${new Date().toISOString()} | 200 | ${duration}ms | POST "translate");

const responseData = {
  code: 200,
  data: result.text, // 取第一个翻译结果
  id: Math.floor(Math.random() * 10000000000), // 生成一个随机 ID
  method: 'Free',
  source_lang,
  target_lang,
  alternatives: result.alternatives
};

res.json(responseData);

} catch (error) {
const duration = Date.now() - startTime;
console.error([ERROR] ${new Date().toISOString()} | 500 | ${duration}ms | POST "translate" | ${error.message});
res.status(500).json({
code: 500,
message: 'Translation failed',
error: error.message
});
}
};

async function get(req, res) {
res.json({
code: 200,
message: "Welcome to the DeepL Free API. Please POST to '/translate'. Visit 'https://github.com/OwO-Network/DeepLX' and 'https://github.com/guobao2333/DeepLX-Serverless' for more information."
});
};

// 启动本地服务器
app.listen(PORT, () => {
console.log(Server is running on http://localhost:${PORT});
});

export { post, get };

需要安装 cors 模块
npm install cors

@guobao2333
Copy link
Owner

guobao2333 commented Sep 3, 2024

我实在看不懂你上面这一段到底在表达什么,看起来像是把代码复制了一份过来……如果你想说的能跟你的标题一样就好了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants