Skip to content

Commit ea8bfe8

Browse files
committed
feat: 还原分词和翻译API
1 parent f3e71ce commit ea8bfe8

File tree

13 files changed

+452
-23
lines changed

13 files changed

+452
-23
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
22

3+
## Endpoint
4+
5+
[/api/xmlrpc](https://api.terwer.space/api/xmlrpc)
6+
7+
[/api/translate?q=](https://api.terwer.space/api/translate?q=)
8+
9+
[/api/jieba?q=](https://api.terwer.space/api/jieba?q=)
10+
11+
## 最近更新
12+
13+
### 2022-07-27
14+
15+
1、新增中文分词服务
16+
17+
### 2022-07-24
18+
19+
1、新增Google翻译服务
20+
321
## Getting Started
422

523
First, run the development server:

next.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ console.log("react path=>", moduleDir("react"))
1313

1414
/**
1515
* @type {import('next').NextConfig}
16-
* @see https://nextjs.org/docs/api-reference/next.config.js/
16+
* @see https://nextjs.org/docs/api-reference/next.config.js/introduction
1717
**/
1818
const nextConfig: NextConfig = {
1919
reactStrictMode: true,

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"lint": "next lint"
1313
},
1414
"dependencies": {
15+
"cors": "^2.8.5",
1516
"next": "13.1.1",
17+
"nodejieba": "^2.6.0",
1618
"react": "18.2.0",
1719
"react-dom": "18.2.0",
1820
"typescript": "4.9.4"

pages/api/cors.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Cors from "cors"
2+
import initMiddleware from "../../utils/cors/init-middleware"
3+
import { NextApiRequest, NextApiResponse } from "next"
4+
5+
// Initialize the cors middleware
6+
const cors = initMiddleware(
7+
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
8+
Cors({
9+
// Only allow requests with GET, POST and OPTIONS
10+
methods: ["GET", "POST", "OPTIONS"],
11+
})
12+
)
13+
14+
export default async function handler(
15+
req: NextApiRequest,
16+
res: NextApiResponse
17+
) {
18+
// Run cors
19+
await cors(req, res)
20+
21+
// Rest of the API logic
22+
res.json({ message: "Hello Everyone!" })
23+
}

pages/api/jieba.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2+
3+
import nodejieba from "nodejieba"
4+
import Cors from "cors"
5+
import initMiddleware from "../../utils/cors/init-middleware"
6+
import { NextApiRequest, NextApiResponse } from "next"
7+
8+
// Initialize the cors middleware
9+
const cors = initMiddleware(
10+
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
11+
Cors({
12+
// Only allow requests with GET, POST and OPTIONS
13+
methods: ["GET", "POST", "OPTIONS"],
14+
})
15+
)
16+
17+
export default async function handler(
18+
req: NextApiRequest,
19+
res: NextApiResponse
20+
) {
21+
// Run cors
22+
await cors(req, res)
23+
24+
let json
25+
const q = req.query.q || ""
26+
if (q instanceof Array) {
27+
throw new Error("参数类型错误")
28+
}
29+
30+
if (!q || q === "") {
31+
json = { result: [] }
32+
res.json(json)
33+
return
34+
}
35+
36+
const result = nodejieba.cut(q)
37+
json = { result: result }
38+
39+
// Rest of the API logic
40+
res.json(json)
41+
}

pages/api/middleware.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Cors from "cors"
2+
import initMiddleware from "../../utils/cors/init-middleware"
3+
// import { SimpleXmlRpcClient } from "simple-xmlrpc"
4+
import { NextApiRequest, NextApiResponse } from "next"
5+
6+
// Initialize the cors middleware
7+
const cors = initMiddleware(
8+
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
9+
Cors({
10+
// Only allow requests with GET, POST and OPTIONS
11+
methods: ["GET", "POST", "OPTIONS"],
12+
})
13+
)
14+
15+
export default async function handler(
16+
req: NextApiRequest,
17+
res: NextApiResponse
18+
) {
19+
// Run cors
20+
await cors(req, res)
21+
22+
let result
23+
result = {
24+
test: "test",
25+
}
26+
27+
// Rest of the API logic
28+
res.status(200).json(result)
29+
}

pages/api/translate.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Cors from "cors"
2+
import initMiddleware from "../../utils/cors/init-middleware"
3+
import { NextApiRequest, NextApiResponse } from "next"
4+
5+
// Initialize the cors middleware
6+
const cors = initMiddleware(
7+
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
8+
Cors({
9+
// Only allow requests with GET, POST and OPTIONS
10+
methods: ["GET", "POST", "OPTIONS"],
11+
})
12+
)
13+
14+
export default async function handler(
15+
req: NextApiRequest,
16+
res: NextApiResponse
17+
) {
18+
// Run cors
19+
await cors(req, res)
20+
21+
const q = req.query.q
22+
const v = await fetch(
23+
"https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=auto&tl=en-US&q=" +
24+
q
25+
)
26+
let json = await v.json()
27+
28+
// Rest of the API logic
29+
res.json(json)
30+
}

0 commit comments

Comments
 (0)