// Student List data/students.json
[
{
"id": 1,
"name": "小红",
"grade": 1
},
{
"id": 2,
"name": "小明",
"grade": 2
},
{
"id": 3,
"name": "小李",
"grade": 3
}
]
// Teacher List data/teachers.json
[
{
"id": 1,
"name": "小野",
"subject": "语文",
"like": 0
},
{
"id": 2,
"name": "小夏",
"subject": "数学",
"like": 0
},
{
"id": 3,
"name": "哈默",
"subject": "英语",
"like": 0
}
]
const express = require('express');
const { readFileSync } = require('fs');
const { resolve } = require('path');
const app = express();
app.all('*', function (req, res, next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'POST,GET');
next();
});
app.get('/getTeachers', function(req, res){
const teacherData = JSON.parse(readFileSync(resolve(__dirname, './data/teachers.json'), 'utf8'));
res.send(teacherData);
});
app.get('/getStudents', function(req, res){
const studentData = JSON.parse(readFileSync(resolve(__dirname, './data/students.json'), 'utf8'));
res.send(studentData);
})
app.listen(8080, function(){
console.log('Welcome to use Express!!!');
})