The method mentioned above was fine until we had to register the plugin, but some modifications to the header were necessary when the plugin was executed.
- Header
- ‘Access-Control-Allow-Credentials’: ‘false’,
- ‘Access-Control-Allow-Headers’: ‘Content-Type, Authorization, openai-conversation-id, openai-ephemeral-user-id’,
- ‘Cache-Control’: ‘no-store, max-age=0’,
Final Code
const http = require('http');
const fs = require('fs');
const path = require('path');
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif'
};
const server = http.createServer((req, res) => {
const filePath = `.${req.url}`;
const extname = String(path.extname(filePath)).toLowerCase();
const contentType = mimeTypes[extname] || 'application/octet-stream';
console.log(req.url)
// Preflight request. Reply successfully:
if (req.method === 'OPTIONS') {
res.writeHead(200, {
'Access-Control-Allow-Origin': 'https://chat.openai.com',
'Access-Control-Allow-Methods': 'GET, POST',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, openai-conversation-id, openai-ephemeral-user-id',
'Access-Control-Allow-Credentials': 'false',
'Cache-Control': 'no-store, max-age=0',
});
res.end();
return;
}
if (fs.existsSync(filePath)) {
// ファイルを読み込んでJSONとして解析します
fs.readFile(filePath, (err, data) => {
if (err) {
// エラーが発生した場合は404 Not Foundを返します
res.writeHead(404, {
'Access-Control-Allow-Origin': 'https://chat.openai.com',
'Access-Control-Allow-Credentials': 'false',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, openai-conversation-id, openai-ephemeral-user-id',
'Cache-Control': 'no-store, max-age=0',
});
res.end();
} else {
// JSONを返します
res.writeHead(200, {
'Content-Type': contentType,
'Access-Control-Allow-Origin': 'https://chat.openai.com',
'Access-Control-Allow-Credentials': 'false',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, openai-conversation-id, openai-ephemeral-user-id',
'Cache-Control': 'no-store, max-age=0',
});
res.end(data);
}
});
} else {
// その他のリクエストには404 Not Foundを返します
res.writeHead(404, {
'Content-Type': 'text/html',
'Access-Control-Allow-Origin': 'https://chat.openai.com',
'Access-Control-Allow-Credentials': 'false',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, openai-conversation-id, openai-ephemeral-user-id',
'Cache-Control': 'no-store, max-age=0',
});
res.end("404 Not Found");
}
});
const port = 3333;
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
To call the TODO, add the following code:
if ("/todos" === req.url) {
console.log("OK")
// JSONを返します
res.writeHead(200, {
'Content-Type': "application/json",
'Access-Control-Allow-Origin': 'https://chat.openai.com',
'Access-Control-Allow-Credentials': 'false',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, openai-conversation-id, openai-ephemeral-user-id',
'Cache-Control': 'no-store, max-age=0',
});
res.end(JSON.stringify({
todos: [
"any task..."
]
}));
return;
}
if (fs.existsSync(filePath)) {
...