route.js

    1. 为了预防报错,采用try catch
    2. 采用同步读取文件 ```javascript // 导出函数 exports.xx = function(xx) {xx} const fs = require(“fs”); const http = require(“http”); const path = require(“path”); // 引入自定义函数,直接引入整个文件名 const url = require(“url”);

    // 私有方法 let getMime = function (suffix) { return new Promise((resolve, reject) => { // 同步方法 var data = fs.readFileSync(“./data/mime.json”); let mimeObj = JSON.parse(data.toString()); return mimeObj[extname]; // fs.readFile(“./data/mime.json”, (err, data) => { // if (err) { // // console.log(err); // reject(err); // return; // } else { // // data会是buffer字符,所以转成字符串,data.toString() // // 将json解析为JSON.parse,并且赋值给Json // let Json = JSON.parse(data.toString()); // // Json后缀名是通过数组的形式呈现的,所以用Json[xx]; // // console.log(Json[suffix]); // resolve(Json[suffix]); // } // }); }); };

    // 文件放在static文件夹里面,就可以实现文件自动读取和下载 // 外部读取时,采用文件名.static来使用该函数 exports.static = function (req, res, staticPath) { let pathname = url.parse(req.url).pathname; // console.log(pathname); pathname = pathname == “/“ ? “/index.html” : pathname;

    // 获取后缀名 let extname = path.extname(pathname); // 2.通过fs模块读取文件 if (pathname != “/favicon.ico”) { // 使用fs读取文件,路径+文件名,还有处理函数 // 通过读取文件,来实现对服务器静态资源的访问 // if (err) { // // err里面也包含头文件和end // res.writeHead(404, { “Content-Type”: ‘text/html ;charset=”utf-8”‘ });

    1. // res.end("404 文件不存在");
    2. // }
    3. // if (!err) {
    4. // let mime = await getMime(extname);
    5. // res.writeHead(200, {
    6. // "Content-Type": "" + mime + ' ;charset="utf-8"',
    7. // });
    8. // res.end(data);
    9. // }
    10. //为了预防报错,采用try catch
    11. try {
    12. // 采用同步读取文件
    13. let data = fs.readFileSync("./" + staticPath + pathname);
    14. if (data) {
    15. let mime = getMime(extname);
    16. res.writeHead(200, {
    17. "Content-Type": "" + mime + ' ;charset="utf-8"',
    18. });
    19. res.end(data);
    20. }
    21. } catch (error) {}
    22. // 需要使用第三方的函数时,使用文件名.函数()

    } };

    1. **app.js**
    2. 1. 创建静态web服务
    3. 1. 路由
    4. 1. 需要配置头文件
    5. **代码**
    6. ```javascript
    7. const http = require("http");
    8. const fs = require("fs");
    9. const path = require("path");
    10. // 引入自定义函数,直接引入整个文件名
    11. const route = require("./module/route");
    12. const url = require('url');
    13. http
    14. // req是一个对象,包含有关引发事件的 HTTP 请求的信息。响应req,您使用res发送回所需的 HTTP 响应。
    15. .createServer(function (req, res) {
    16. //创建静态web服务
    17. route.static(req, res, "static");
    18. // 路由
    19. let pathname = url.parse(req.url).pathname;
    20. if(pathname == '/login') {
    21. // 需要配置头文件
    22. res.writeHead(200, {
    23. "Content-Type": 'text/html;charset="utf-8"',
    24. });
    25. res.end('处理登录');
    26. }
    27. else if(pathname == '/register') {
    28. res.writeHead(200, {
    29. "Content-Type": 'text/html;charset="utf-8"',
    30. });
    31. res.end('处理注册');
    32. }
    33. else if(pathname == '/order') {
    34. res.writeHead(200, {
    35. "Content-Type": 'text/html;charset="utf-8"',
    36. });
    37. res.end('处理预定');
    38. }
    39. else {
    40. res.writeHead(404, {
    41. "Content-Type": 'text/html;charset="utf-8"',
    42. });
    43. res.end('页面不存在');
    44. }
    45. })
    46. .listen(4000);
    47. console.log("server running at http://127.0.0.1:4000/");