Compare commits

..

10 Commits

Author SHA1 Message Date
a45a9a6f2d 添加cors 2023-09-02 01:46:18 +08:00
5c80e7cf23 修改获取思路 2023-09-02 01:34:01 +08:00
e33430e08c 增加了获取所有网站的方式 2023-09-02 01:25:53 +08:00
4b1a0b0078 修复输出sql bug 2023-09-02 01:01:05 +08:00
4dbe7c5893 排序逻辑 2023-09-02 00:59:33 +08:00
afda4d6558 更改排序逻辑 2023-09-02 00:52:11 +08:00
7305fc7a88 更改排序方式 2023-09-02 00:48:26 +08:00
62f93b1b90 增加数据库安全性 2023-09-02 00:43:53 +08:00
2dc0a7069a 更改排序方式 2023-09-02 00:43:35 +08:00
ff40cc6a27 修复判断bug 2023-09-02 00:38:53 +08:00
7 changed files with 98 additions and 19 deletions

23
package-lock.json generated
View File

@ -9,12 +9,14 @@
"version": "0.0.1",
"dependencies": {
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"express": "^4.17.2",
"mysql": "^2.14.1",
"reflect-metadata": "^0.1.13",
"typeorm": "0.3.17"
},
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/node": "^16.11.10",
"ts-node": "10.7.0",
@ -101,6 +103,15 @@
"@types/node": "*"
}
},
"node_modules/@types/cors": {
"version": "2.8.13",
"resolved": "https://registry.npmmirror.com/@types/cors/-/cors-2.8.13.tgz",
"integrity": "sha512-RG8AStHlUiV5ysZQKq97copd2UmVYw3/pRMLefISZ3S1hK104Cwm7iLQ3fTKx+lsUH2CE8FlLaYeEA2LSeqYUA==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/express": {
"version": "4.17.17",
"resolved": "https://registry.npmmirror.com/@types/express/-/express-4.17.17.tgz",
@ -460,6 +471,18 @@
"resolved": "https://registry.npmmirror.com/core-util-is/-/core-util-is-1.0.3.tgz",
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
},
"node_modules/cors": {
"version": "2.8.5",
"resolved": "https://registry.npmmirror.com/cors/-/cors-2.8.5.tgz",
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
"dependencies": {
"object-assign": "^4",
"vary": "^1"
},
"engines": {
"node": ">= 0.10"
}
},
"node_modules/create-require": {
"version": "1.1.1",
"resolved": "https://registry.npmmirror.com/create-require/-/create-require-1.1.1.tgz",

View File

@ -4,6 +4,7 @@
"description": "Awesome project developed with TypeORM.",
"type": "commonjs",
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/node": "^16.11.10",
"ts-node": "10.7.0",
@ -11,6 +12,7 @@
},
"dependencies": {
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"express": "^4.17.2",
"mysql": "^2.14.1",
"reflect-metadata": "^0.1.13",

View File

@ -0,0 +1,11 @@
import { Request, Response, NextFunction } from "express";
import { AppDataSource } from "../data-source";
import { Web } from "../entity/Web";
export class GetWebsController{
async all(request: Request, response: Response, next: NextFunction) {
AppDataSource.getRepository(Web).find().then((data)=>{
return response.json(data.map(d=>d.name))
})
}
}

View File

@ -1,37 +1,65 @@
import { AppDataSource } from "../data-source";
import { Times } from "../entity/Times";
import { NextFunction, Request, Response } from "express"
import { Web } from "../entity/Web";
export class HotTopController{
export class HotTopController {
private TimesRepository = AppDataSource.getRepository(Times)
async all(request: Request, response: Response, next: NextFunction){
async all(request: Request, response: Response, next: NextFunction) {
const name = request.params.name
const searchWeb = await AppDataSource.getRepository(Web).findOne({
where:{
name:name
}
})
if(searchWeb == null){
response.status(404)
return "error"
}
const nowDate = new Date()
const yestDay = new Date()
let hours = 1
let rawHours = parseInt(request.query.hours as unknown as string)
if(request.query.hours && !isNaN(rawHours) && rawHours <=24 && rawHours > 0)
if (request.query.hours !=undefined && !isNaN(rawHours) && rawHours <= 24 && rawHours > 0) {
hours = parseInt(request.query.hours as unknown as string)
}
else {
response.status(404)
return "error"
}
yestDay.setHours(nowDate.getHours() - hours)
// console.log(yestDay.toLocaleString())
let limit:number
if(request.query.limit) limit = parseInt(request.query.limit as unknown as string)
if(isNaN(limit) || limit>50 || limit<1) limit = 10
let limit: number
if (request.query.limit == undefined) {
response.status(404)
return "error"
}
limit = Number.parseInt(request.query.limit as string)
if (isNaN(limit) || limit > 50 || limit < 1) {
response.status(404)
return "error"
}
// console.log(limit,hours)
// return "OK"
return this.TimesRepository
.createQueryBuilder("time")
.leftJoinAndSelect("time.fromWeb", "fromWeb")
.where("time.lastTime BETWEEN :beginTime AND :endTime",
{
beginTime:yestDay,
endTime:nowDate
}
)
.addOrderBy('time.times',"DESC")
.createQueryBuilder("times")
.leftJoinAndSelect("times.fromWeb", "fromWeb")
.where("times.lastTime BETWEEN :beginTime AND :endTime",
{
beginTime: yestDay,
endTime: nowDate
}
)
.andWhere("times.fromWeb = :searchWeb",{
searchWeb:searchWeb.id
})
.addOrderBy('times.times', "DESC")
.addOrderBy('times.lastTime', "DESC")
.limit(limit)
// .getSql()
.getMany()
}
}

View File

@ -16,7 +16,7 @@ export const AppDataSource = new DataSource({
password: "ZengtudorRXR2008",
// password: "Zengtudor",
database: "hot",
synchronize: true,
synchronize: false,
logging: false,
entities: [Search,Hot,Web,Times],
migrations: [],

View File

@ -3,12 +3,20 @@ import * as bodyParser from "body-parser"
import { Request, Response } from "express"
import { AppDataSource } from "./data-source"
import { Routes } from "./routes"
import * as cors from "cors"
AppDataSource.initialize().then(async () => {
// create express app
const app = express()
app.use(bodyParser.json())
app.use(cors({
origin:[
"https://www.zziyu.cn",
"http://localhost:3000"
],
credentials:true //每次登陆都验证跨域请求,要不会每次报跨域错误
}));
// register express routes from defined application routes
Routes.forEach(route => {
@ -16,7 +24,6 @@ AppDataSource.initialize().then(async () => {
const result = (new (route.controller as any))[route.action](req, res, next)
if (result instanceof Promise) {
result.then(result => result !== null && result !== undefined ? res.send(result) : undefined)
} else if (result !== null && result !== undefined) {
res.json(result)
}
@ -49,3 +56,4 @@ AppDataSource.initialize().then(async () => {
console.log("Express server has started on port 3501. Open http://localhost:3501/ to see results")
}).catch(error => console.log(error))

View File

@ -1,3 +1,4 @@
import { GetWebsController } from "./controller/GetWebsController";
import { HotTopController } from "./controller/HotTopController";
@ -5,10 +6,16 @@ import { HotTopController } from "./controller/HotTopController";
export const Routes = [
{
method: "get",
route: "/hot/top/all",
route: "/hot/top/:name",
controller: HotTopController,
action: "all"
},
{
method: "get",
route: "/hot/webs",
controller: GetWebsController,
action: "all"
},
// {
// method: "get",
// route: "/users",