1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
epp/backend-mock/index.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-11-22 12:44:40 +08:00
// 1. 导入http模块
const http = require("http");
// 2. 创建一个web服务器对象
const server = http.createServer();
// 3. 监听请求事件
server.on("request", (req, res) => {
//req-->request 请求对象, res-->response 响应对象
// 通过响应头设置返回前台数据格式及编码。(解决中文乱码的问题)
// res.setHeader('Content-Type', 'text/html;charset=utf-8');
//res.write()表示向客户端输出的方法
// res.write("hello world你好nodejs")
res.setHeader('Content-Type', 'text/json;charset=utf-8');
let result = {};
if (req.url.startsWith('/user/login')) {
result = {
2022-11-23 20:37:35 +08:00
success: true,
2022-11-22 12:44:40 +08:00
msg: "登录成功",
2022-11-23 20:37:35 +08:00
data: {
userInfo: {
"id": 1,
"username": "root",
"realname": "管理员",
"idNumber": null,
"phoneNumber": null,
"role": 1,
"buildingId": null,
"doorplate": null,
"permission": "0",
"permissionTime": null
}
2022-11-22 12:44:40 +08:00
}
2022-11-23 20:37:35 +08:00
// success: false,
2022-11-22 12:44:40 +08:00
// msg: "用户名或密码不正确",
2022-11-23 20:37:35 +08:00
// data: null
2022-11-22 12:44:40 +08:00
};
} else {
result = {
code: 500,
msg: "服务器内部错误",
extra: {
url: req.url,
method: req.method,
headers: req.headers,
req: Object.keys(req),
res: Object.keys(res)
}
};
}
res.write(JSON.stringify(result));
//res.end()每次响应完,需要调用此方法 来结束响束
res.end();
})
// 4. 监听端口为了避免端口冲突这里给一个本机端口3000
server.listen(8080, () => {
let baseUrl = 'http://localhost:8080';
console.log(`服务启动成功: ${baseUrl}/`);
console.log();
console.log(`${baseUrl}/user/login`);
})