问题描述
responses.js
const TYPE_JSON = 'application/json';
const STATUS_CODE_OK = 200;
const STATUS_CODE_CREATED_SUCCESSFULLY = 201;
const STATUS_CODE_BAD_REQUEST = 400;
const STATUS_CODE_UNAUTHORIZED = 401;
const STATUS_CODE_NOT_FOUND = 404;
const STATUS_CODE_SERVER_ERROR = 500;
const jsonOK = function (data,message) {
const status = STATUS_CODE_OK;
message = message ? message : 'Successful request.';
this.status(STATUS_CODE_OK);
this.type(TYPE_JSON);
return this.json({ message,data,status: status });
};
const jsonCreated = function (data,message) {
const status = STATUS_CODE_CREATED_SUCCESSFULLY;
message = message ? message : 'Created successfully.';
this.status(STATUS_CODE_CREATED_SUCCESSFULLY);
this.type(TYPE_JSON);
return this.json({ message,status: status });
}
const jsonBadRequest = function (data,message) {
const status = STATUS_CODE_BAD_REQUEST;
message = message ? message : 'Bad request.';
this.status(STATUS_CODE_BAD_REQUEST);
this.type(TYPE_JSON);
return this.json({ message,status: status });
};
const jsonNotFound = function (data,message) {
const status = STATUS_CODE_NOT_FOUND;
message = message ? message : 'Not found.';
this.status(STATUS_CODE_NOT_FOUND);
this.type(TYPE_JSON);
return this.json({ message,status: status });
};
const response = (req,res,next) => {
res.jsonOK = jsonOK;
res.jsonCreated = jsonCreated;
res.jsonBadRequest = jsonBadRequest;
next();
};
module.exports = response;
所以在我的控制器中,我这样称呼它:
router.get('/',(request,response) => {
return response.jsonOK();
})
但是最近我开始将项目迁移到打字稿,所以...
import { Request,Response,NextFunction } from 'express';
const TYPE_JSON = 'application/json';
const STATUS_CODE_OK = 200;
const STATUS_CODE_CREATED_SUCCESSFULLY = 201;
const STATUS_CODE_BAD_REQUEST = 400;
const STATUS_CODE_UNAUTHORIZED = 401;
const STATUS_CODE_NOT_FOUND = 404;
const STATUS_CODE_SERVER_ERROR = 500;
const jsonOK = function (data: any,message: string) {
const status = STATUS_CODE_OK;
message = message ? message : 'Successful request.';
this.status(STATUS_CODE_OK);
this.type(TYPE_JSON);
return this.json({ message,status: status });
};
const jsonCreated = function (data: any,message: string) {
const status = STATUS_CODE_CREATED_SUCCESSFULLY;
message = message ? message : 'Created successfully.';
this.status(STATUS_CODE_CREATED_SUCCESSFULLY);
this.type(TYPE_JSON);
return this.json({ message,status: status });
}
const jsonBadRequest = function (data: any,message: string) {
const status = STATUS_CODE_BAD_REQUEST;
message = message ? message : 'Bad request.';
this.status(STATUS_CODE_BAD_REQUEST);
this.type(TYPE_JSON);
return this.json({ message,status: status });
};
const jsonNotFound = function (data: any,message: string) {
const status = STATUS_CODE_NOT_FOUND;
message = message ? message : 'Not found.';
this.status(STATUS_CODE_NOT_FOUND);
this.type(TYPE_JSON);
return this.json({ message,status: status });
};
const response = (req: Request,res: Response,next: NextFunction) => {
res.jsonOK = jsonOK;
res.jsonCreated = jsonCreated;
res.jsonBadRequest = jsonBadRequest;
next();
};
module.exports = response;
但是它抱怨很多事情,例如:
- 这个。具有任何隐式类型 在响应类型中找不到
- jsonOK,jsonCreated,jsonBadRequest
所以什么都没有了,我该如何解决?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)