-
案例目标:实现GET的主要属性为req.method
- 使用nodejs,实现一个Ajax的get请求方式
- 将lib里的data.json文件请求获取出来
- data里面的数据为
[{"小红":"05"},{"小李":"11"},{"小四":"11"}]
-
案例准备
-
案例代码
// 1.定义核心模块 http fs path
const http = require('http')
const fs = require('fs')
const path = require('path')
// 2.创建服务
const server = http.createServer((req, res) => {
// 2.1 如果url地址为/getList 执行代码
// -req.method 为请求类型
if (req.url === '/getList' && req.method === 'GET') {
// 2.2 拼接字符串,为读取数据