1.创建一个新的项目目录:在你想要保存项目的位置创建一个新文件夹。
2.打开终端:进入到项目文件夹,并打开终端。
3.初始化项目:运行以下命令来初始化新的 Node.js 项目,并按照提示进行配置:
npm init
这将创建一个 package.json 文件,其中包含了项目的基本信息和依赖项。
4.安装所需模块:Express
npm install express
5.创建主要文件:在项目文件夹中创建一个主要的 Node.js 文件(例如 index.js),这将是你的应用程序的入口点。
6.编写代码:打开主要文件 index.js,并编写你的 Node.js 服务代码。例如,你可以创建一个简单的 HTTP 服务器:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
})
const port = 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
7.安装反向代理模块:
npm install http-proxy-middleware
8.在你的Node.js文件中导入所需的库并设置代理:
const express = require('express')
const {
createProxyMiddleware
} = require('http-proxy-middleware');
const app = express()
const port = 3000
app.use('/', createProxyMiddleware({
target: 'https://api.openai.com',
changeOrigin: true,
secure:false,
onProxyRes: function (proxyRes, req, res) {
proxyRes.headers['Access-Control-Allow-Origin'] = '*';
}
}));
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
9.在package.json中设置启动命令:
"scripts": {
"start": "node ./bin/www"
}
10.使用npm start启动项目,使用本地地址就能访问到chatgpt了。
直接下载地址:https://github.com/Ice-Hazymoon/openai-scf-proxy/releases/download/0.0.3/openai-proxy.zip;