发帖路由
This commit is contained in:
父節點
09ab33cdac
當前提交
edf4e11eb0
@ -18,6 +18,7 @@
|
|||||||
"@types/jsonwebtoken": "^8.5.4",
|
"@types/jsonwebtoken": "^8.5.4",
|
||||||
"@types/node": "^16.4.1",
|
"@types/node": "^16.4.1",
|
||||||
"@types/tough-cookie": "^4.0.1",
|
"@types/tough-cookie": "^4.0.1",
|
||||||
|
"@types/validator": "^13.6.3",
|
||||||
"axios": "^0.21.1",
|
"axios": "^0.21.1",
|
||||||
"axios-cookiejar-support": "^1.0.1",
|
"axios-cookiejar-support": "^1.0.1",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
@ -31,6 +32,7 @@
|
|||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"tough-cookie": "^4.0.0",
|
"tough-cookie": "^4.0.0",
|
||||||
"ts-node": "^10.1.0",
|
"ts-node": "^10.1.0",
|
||||||
"typedi": "^0.10.0"
|
"typedi": "^0.10.0",
|
||||||
|
"validator": "^13.6.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import {getPoolCreatingPromise} from "./includes"
|
|||||||
import expressJWT from 'express-jwt'
|
import expressJWT from 'express-jwt'
|
||||||
import type {ErrorRequestHandler} from "express"
|
import type {ErrorRequestHandler} from "express"
|
||||||
import {IResultJson} from "./types"
|
import {IResultJson} from "./types"
|
||||||
|
import {postRouter} from "./routers/post-router"
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
@ -19,9 +20,9 @@ app.use(
|
|||||||
)
|
)
|
||||||
const authErrorHandler: ErrorRequestHandler =
|
const authErrorHandler: ErrorRequestHandler =
|
||||||
(err,
|
(err,
|
||||||
req
|
req,
|
||||||
, res
|
res,
|
||||||
, next) => {
|
next) => {
|
||||||
if (err.name == 'UnauthorizedError') {
|
if (err.name == 'UnauthorizedError') {
|
||||||
res.status(401).send(<IResultJson>{status: false, data: '使用者未登入'})
|
res.status(401).send(<IResultJson>{status: false, data: '使用者未登入'})
|
||||||
}
|
}
|
||||||
@ -29,6 +30,7 @@ const authErrorHandler: ErrorRequestHandler =
|
|||||||
app.use(authErrorHandler)
|
app.use(authErrorHandler)
|
||||||
|
|
||||||
app.use('/user', userRouter)
|
app.use('/user', userRouter)
|
||||||
|
app.use('/post',postRouter)
|
||||||
|
|
||||||
async function entrypoint() {
|
async function entrypoint() {
|
||||||
await getPoolCreatingPromise()
|
await getPoolCreatingPromise()
|
||||||
|
7
src/models/post-model.ts
Normal file
7
src/models/post-model.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import 'reflect-metadata'
|
||||||
|
import {Service} from "typedi"
|
||||||
|
|
||||||
|
@Service()
|
||||||
|
export class PostModel{
|
||||||
|
|
||||||
|
}
|
@ -24,4 +24,17 @@ export class UserModel {
|
|||||||
role: user[0].role
|
role: user[0].role
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findUserById(id: number) {
|
||||||
|
let user = await db.query('select * from users where id=?', [id])
|
||||||
|
if (!user.length) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return <IUser>{
|
||||||
|
id: user[0].id,
|
||||||
|
username: user[0].username,
|
||||||
|
realName: user[0].realName,
|
||||||
|
role: user[0].role
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
34
src/routers/post-router.ts
Normal file
34
src/routers/post-router.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import 'reflect-metadata'
|
||||||
|
import {Container} from "typedi"
|
||||||
|
import {PostService} from "../services/post-service"
|
||||||
|
import express from "express"
|
||||||
|
import {body} from "express-validator"
|
||||||
|
import * as Validator from "validator"
|
||||||
|
import {hasValidationErrors} from "../includes"
|
||||||
|
|
||||||
|
let postService = Container.get(PostService)
|
||||||
|
|
||||||
|
let postRouter = express.Router()
|
||||||
|
postRouter.post('/submit',
|
||||||
|
body('content').notEmpty().withMessage('请输入内容')
|
||||||
|
.escape(),
|
||||||
|
body('image').isJSON().withMessage('图片必须为JSON格式').bail().custom((input, {req}) => {
|
||||||
|
let arr: string[] = JSON.parse(input)
|
||||||
|
if(!arr.length){
|
||||||
|
throw new Error('图片数量至少为1')
|
||||||
|
}
|
||||||
|
for (let url of arr) {
|
||||||
|
if (!Validator.default.isURL(url, {host_whitelist: [/.*\.360buyimg\.com/]})) {
|
||||||
|
throw new Error('必须是360buyimg.com下的图片')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
req.body.imageArr = arr
|
||||||
|
return true
|
||||||
|
}),
|
||||||
|
(req: express.Request, res: express.Response) => {
|
||||||
|
if (hasValidationErrors(req, res)) return
|
||||||
|
res.json(postService.submit(req.body.content, req.body.imageArr))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export {postRouter}
|
10
src/services/post-service.ts
Normal file
10
src/services/post-service.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import 'reflect-metadata'
|
||||||
|
import {Service} from "typedi"
|
||||||
|
|
||||||
|
@Service()
|
||||||
|
export class PostService {
|
||||||
|
async submit(content: string, imageArr: string[]) {
|
||||||
|
console.log(content)
|
||||||
|
console.log(imageArr)
|
||||||
|
}
|
||||||
|
}
|
@ -50,10 +50,8 @@ export class UserService {
|
|||||||
return resultJson.success(token)
|
return resultJson.success(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
async me(user: JWTUserPayload) {
|
async me(jwtUserPayload: JWTUserPayload) {
|
||||||
return resultJson.success({
|
let user=await this.userModel.findUserById(jwtUserPayload.id)
|
||||||
id: user.id,
|
return resultJson.success(user)
|
||||||
role: user.role
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
載入中…
x
新增問題並參考
Block a user