Compare commits

..

No commits in common. "a45a9a6f2d6dc5cbbb971b17408703bd460f0f74" and "56b1a8617fef59d896c027a1c334468d03c9cf1f" have entirely different histories.

7 changed files with 19 additions and 98 deletions

23
package-lock.json generated
View File

@ -9,14 +9,12 @@
"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",
@ -103,15 +101,6 @@
"@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",
@ -471,18 +460,6 @@
"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,7 +4,6 @@
"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",
@ -12,7 +11,6 @@
},
"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

@ -1,11 +0,0 @@
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,65 +1,37 @@
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) {
const name = request.params.name
const searchWeb = await AppDataSource.getRepository(Web).findOne({
where:{
name:name
}
})
if(searchWeb == null){
response.status(404)
return "error"
}
async all(request: Request, response: Response, next: NextFunction){
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 !=undefined && !isNaN(rawHours) && rawHours <= 24 && rawHours > 0) {
if(request.query.hours && !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 == 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"
}
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
// console.log(limit,hours)
// return "OK"
return this.TimesRepository
.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")
.createQueryBuilder("time")
.leftJoinAndSelect("time.fromWeb", "fromWeb")
.where("time.lastTime BETWEEN :beginTime AND :endTime",
{
beginTime:yestDay,
endTime:nowDate
}
)
.addOrderBy('time.times',"DESC")
.limit(limit)
// .getSql()
.getMany()
}
}

View File

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

View File

@ -3,20 +3,12 @@ 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 => {
@ -24,6 +16,7 @@ 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)
}
@ -56,4 +49,3 @@ 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,4 +1,3 @@
import { GetWebsController } from "./controller/GetWebsController";
import { HotTopController } from "./controller/HotTopController";
@ -6,16 +5,10 @@ import { HotTopController } from "./controller/HotTopController";
export const Routes = [
{
method: "get",
route: "/hot/top/:name",
route: "/hot/top/all",
controller: HotTopController,
action: "all"
},
{
method: "get",
route: "/hot/webs",
controller: GetWebsController,
action: "all"
},
// {
// method: "get",
// route: "/users",