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 Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 = {
success: true,
msg: "登录成功",
data: {
userInfo: {
"id": 1,
"username": "root",
"realname": "管理员",
"idNumber": null,
"phoneNumber": null,
"role": 1,
"buildingId": null,
"doorplate": null,
"permission": "0",
"permissionTime": null
}
}
// success: false,
// msg: "用户名或密码不正确",
// data: null
};
} 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`);
})