1. // Student List data/students.json
    2. [
    3. {
    4. "id": 1,
    5. "name": "小红",
    6. "grade": 1
    7. },
    8. {
    9. "id": 2,
    10. "name": "小明",
    11. "grade": 2
    12. },
    13. {
    14. "id": 3,
    15. "name": "小李",
    16. "grade": 3
    17. }
    18. ]
    19. // Teacher List data/teachers.json
    20. [
    21. {
    22. "id": 1,
    23. "name": "小野",
    24. "subject": "语文",
    25. "like": 0
    26. },
    27. {
    28. "id": 2,
    29. "name": "小夏",
    30. "subject": "数学",
    31. "like": 0
    32. },
    33. {
    34. "id": 3,
    35. "name": "哈默",
    36. "subject": "英语",
    37. "like": 0
    38. }
    39. ]
    1. const express = require('express');
    2. const { readFileSync } = require('fs');
    3. const { resolve } = require('path');
    4. const app = express();
    5. app.all('*', function (req, res, next){
    6. res.header('Access-Control-Allow-Origin', '*');
    7. res.header('Access-Control-Allow-Methods', 'POST,GET');
    8. next();
    9. });
    10. app.get('/getTeachers', function(req, res){
    11. const teacherData = JSON.parse(readFileSync(resolve(__dirname, './data/teachers.json'), 'utf8'));
    12. res.send(teacherData);
    13. });
    14. app.get('/getStudents', function(req, res){
    15. const studentData = JSON.parse(readFileSync(resolve(__dirname, './data/students.json'), 'utf8'));
    16. res.send(studentData);
    17. })
    18. app.listen(8080, function(){
    19. console.log('Welcome to use Express!!!');
    20. })