Compare commits
10 Commits
56b1a8617f
...
a45a9a6f2d
Author | SHA1 | Date | |
---|---|---|---|
a45a9a6f2d | |||
5c80e7cf23 | |||
e33430e08c | |||
4b1a0b0078 | |||
4dbe7c5893 | |||
afda4d6558 | |||
7305fc7a88 | |||
62f93b1b90 | |||
2dc0a7069a | |||
ff40cc6a27 |
23
package-lock.json
generated
23
package-lock.json
generated
@ -9,12 +9,14 @@
|
|||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.19.1",
|
"body-parser": "^1.19.1",
|
||||||
|
"cors": "^2.8.5",
|
||||||
"express": "^4.17.2",
|
"express": "^4.17.2",
|
||||||
"mysql": "^2.14.1",
|
"mysql": "^2.14.1",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"typeorm": "0.3.17"
|
"typeorm": "0.3.17"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/cors": "^2.8.13",
|
||||||
"@types/express": "^4.17.17",
|
"@types/express": "^4.17.17",
|
||||||
"@types/node": "^16.11.10",
|
"@types/node": "^16.11.10",
|
||||||
"ts-node": "10.7.0",
|
"ts-node": "10.7.0",
|
||||||
@ -101,6 +103,15 @@
|
|||||||
"@types/node": "*"
|
"@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": {
|
"node_modules/@types/express": {
|
||||||
"version": "4.17.17",
|
"version": "4.17.17",
|
||||||
"resolved": "https://registry.npmmirror.com/@types/express/-/express-4.17.17.tgz",
|
"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",
|
"resolved": "https://registry.npmmirror.com/core-util-is/-/core-util-is-1.0.3.tgz",
|
||||||
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
|
"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": {
|
"node_modules/create-require": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmmirror.com/create-require/-/create-require-1.1.1.tgz",
|
"resolved": "https://registry.npmmirror.com/create-require/-/create-require-1.1.1.tgz",
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
"description": "Awesome project developed with TypeORM.",
|
"description": "Awesome project developed with TypeORM.",
|
||||||
"type": "commonjs",
|
"type": "commonjs",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/cors": "^2.8.13",
|
||||||
"@types/express": "^4.17.17",
|
"@types/express": "^4.17.17",
|
||||||
"@types/node": "^16.11.10",
|
"@types/node": "^16.11.10",
|
||||||
"ts-node": "10.7.0",
|
"ts-node": "10.7.0",
|
||||||
@ -11,6 +12,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.19.1",
|
"body-parser": "^1.19.1",
|
||||||
|
"cors": "^2.8.5",
|
||||||
"express": "^4.17.2",
|
"express": "^4.17.2",
|
||||||
"mysql": "^2.14.1",
|
"mysql": "^2.14.1",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
|
11
src/controller/GetWebsController.ts
Normal file
11
src/controller/GetWebsController.ts
Normal 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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -1,37 +1,65 @@
|
|||||||
import { AppDataSource } from "../data-source";
|
import { AppDataSource } from "../data-source";
|
||||||
import { Times } from "../entity/Times";
|
import { Times } from "../entity/Times";
|
||||||
import { NextFunction, Request, Response } from "express"
|
import { NextFunction, Request, Response } from "express"
|
||||||
|
import { Web } from "../entity/Web";
|
||||||
|
|
||||||
export class HotTopController{
|
export class HotTopController {
|
||||||
private TimesRepository = AppDataSource.getRepository(Times)
|
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 nowDate = new Date()
|
||||||
const yestDay = new Date()
|
const yestDay = new Date()
|
||||||
|
|
||||||
let hours = 1
|
let hours = 1
|
||||||
let rawHours = parseInt(request.query.hours as unknown as string)
|
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)
|
hours = parseInt(request.query.hours as unknown as string)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
response.status(404)
|
||||||
|
return "error"
|
||||||
|
}
|
||||||
|
|
||||||
yestDay.setHours(nowDate.getHours() - hours)
|
yestDay.setHours(nowDate.getHours() - hours)
|
||||||
// console.log(yestDay.toLocaleString())
|
// console.log(yestDay.toLocaleString())
|
||||||
let limit:number
|
let limit: number
|
||||||
if(request.query.limit) limit = parseInt(request.query.limit as unknown as string)
|
if (request.query.limit == undefined) {
|
||||||
if(isNaN(limit) || limit>50 || limit<1) limit = 10
|
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)
|
// console.log(limit,hours)
|
||||||
// return "OK"
|
// return "OK"
|
||||||
return this.TimesRepository
|
return this.TimesRepository
|
||||||
.createQueryBuilder("time")
|
.createQueryBuilder("times")
|
||||||
.leftJoinAndSelect("time.fromWeb", "fromWeb")
|
.leftJoinAndSelect("times.fromWeb", "fromWeb")
|
||||||
.where("time.lastTime BETWEEN :beginTime AND :endTime",
|
.where("times.lastTime BETWEEN :beginTime AND :endTime",
|
||||||
{
|
{
|
||||||
beginTime:yestDay,
|
beginTime: yestDay,
|
||||||
endTime:nowDate
|
endTime: nowDate
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.addOrderBy('time.times',"DESC")
|
.andWhere("times.fromWeb = :searchWeb",{
|
||||||
|
searchWeb:searchWeb.id
|
||||||
|
})
|
||||||
|
.addOrderBy('times.times', "DESC")
|
||||||
|
.addOrderBy('times.lastTime', "DESC")
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
|
// .getSql()
|
||||||
.getMany()
|
.getMany()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,7 +16,7 @@ export const AppDataSource = new DataSource({
|
|||||||
password: "ZengtudorRXR2008",
|
password: "ZengtudorRXR2008",
|
||||||
// password: "Zengtudor",
|
// password: "Zengtudor",
|
||||||
database: "hot",
|
database: "hot",
|
||||||
synchronize: true,
|
synchronize: false,
|
||||||
logging: false,
|
logging: false,
|
||||||
entities: [Search,Hot,Web,Times],
|
entities: [Search,Hot,Web,Times],
|
||||||
migrations: [],
|
migrations: [],
|
||||||
|
10
src/index.ts
10
src/index.ts
@ -3,12 +3,20 @@ import * as bodyParser from "body-parser"
|
|||||||
import { Request, Response } from "express"
|
import { Request, Response } from "express"
|
||||||
import { AppDataSource } from "./data-source"
|
import { AppDataSource } from "./data-source"
|
||||||
import { Routes } from "./routes"
|
import { Routes } from "./routes"
|
||||||
|
import * as cors from "cors"
|
||||||
|
|
||||||
AppDataSource.initialize().then(async () => {
|
AppDataSource.initialize().then(async () => {
|
||||||
|
|
||||||
// create express app
|
// create express app
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
|
app.use(cors({
|
||||||
|
origin:[
|
||||||
|
"https://www.zziyu.cn",
|
||||||
|
"http://localhost:3000"
|
||||||
|
],
|
||||||
|
credentials:true //每次登陆都验证跨域请求,要不会每次报跨域错误
|
||||||
|
}));
|
||||||
|
|
||||||
// register express routes from defined application routes
|
// register express routes from defined application routes
|
||||||
Routes.forEach(route => {
|
Routes.forEach(route => {
|
||||||
@ -16,7 +24,6 @@ AppDataSource.initialize().then(async () => {
|
|||||||
const result = (new (route.controller as any))[route.action](req, res, next)
|
const result = (new (route.controller as any))[route.action](req, res, next)
|
||||||
if (result instanceof Promise) {
|
if (result instanceof Promise) {
|
||||||
result.then(result => result !== null && result !== undefined ? res.send(result) : undefined)
|
result.then(result => result !== null && result !== undefined ? res.send(result) : undefined)
|
||||||
|
|
||||||
} else if (result !== null && result !== undefined) {
|
} else if (result !== null && result !== undefined) {
|
||||||
res.json(result)
|
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")
|
console.log("Express server has started on port 3501. Open http://localhost:3501/ to see results")
|
||||||
|
|
||||||
}).catch(error => console.log(error))
|
}).catch(error => console.log(error))
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { GetWebsController } from "./controller/GetWebsController";
|
||||||
import { HotTopController } from "./controller/HotTopController";
|
import { HotTopController } from "./controller/HotTopController";
|
||||||
|
|
||||||
|
|
||||||
@ -5,10 +6,16 @@ import { HotTopController } from "./controller/HotTopController";
|
|||||||
export const Routes = [
|
export const Routes = [
|
||||||
{
|
{
|
||||||
method: "get",
|
method: "get",
|
||||||
route: "/hot/top/all",
|
route: "/hot/top/:name",
|
||||||
controller: HotTopController,
|
controller: HotTopController,
|
||||||
action: "all"
|
action: "all"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
method: "get",
|
||||||
|
route: "/hot/webs",
|
||||||
|
controller: GetWebsController,
|
||||||
|
action: "all"
|
||||||
|
},
|
||||||
// {
|
// {
|
||||||
// method: "get",
|
// method: "get",
|
||||||
// route: "/users",
|
// route: "/users",
|
||||||
|
Loading…
Reference in New Issue
Block a user