Skip to content

如何定义全局变量? #47

@aFcFzF

Description

@aFcFzF

// 我想实现按照请求次数返回某些mock数据,但是每次都会触发

let jsonCounter = 0;
let responseData = {};

console.log('执行外部') // 每次请求都会触发

if (jsonCounter === 0) {
responseData = { code: 200, msg: "第一次请求", data: { id: 1, name: "音频数据 1" } };
} else if (jsonCounter === 1) {
responseData = { code: 200, msg: "第二次请求", data: { id: 2, name: "音频数据 2" } };
} else if (jsonCounter === 2) {
responseData = { code: 200, msg: "第三次请求", data: { id: 3, name: "音频数据 3" } };
} else {
responseData = { code: 200, msg: "后续请求", data: { id: jsonCounter, name: 音频数据 ${jsonCounter} } };
jsonCounter = 0;
}

exports.handleRequest = (ctx, next) => {
console.log('内部执行') // 每次都会触发

ctx.res.setHeader('Content-Type', 'application/json; charset=utf-8');
ctx.res.end(JSON.stringify(responseData));
next();
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions