根据url返回不同的文件

  1. import * as http from "http";
  2. import { IncomingMessage, ServerResponse } from "http";
  3. import * as fs from "fs";
  4. import * as p from "path";
  5. // __dirname 表示当前文件所在目录
  6. const server = http.createServer();
  7. const publicDir = p.resolve(__dirname, "public");
  8. // path.resolve() 传入路径或路径片段,解析为绝对路径
  9. server.on("request", (request: IncomingMessage, response: ServerResponse) => {
  10. const { method, url, headers } = request;
  11. switch (url) {
  12. case "/index.html":
  13. response.setHeader("Content-Type", "text/html; chartset=utf-8");
  14. fs.readFile(p.resolve(publicDir, "index.html"), (error, data) => {
  15. if (error) throw error;
  16. response.end(data.toString());
  17. });
  18. break;
  19. case "/style.css":
  20. response.setHeader("Content-Type", "text/css; charset=utf-8");
  21. fs.readFile(p.resolve(publicDir, "style.css"), (error, data) => {
  22. if (error) throw error;
  23. response.end(data.toString());
  24. });
  25. break;
  26. case "/main.js":
  27. response.setHeader("Content-Type", "text/javascript; charset=utf-8");
  28. fs.readFile(p.resolve(publicDir, "main.js"), (error, data) => {
  29. if (error) throw error;
  30. response.end(data.toString());
  31. });
  32. break;
  33. }
  34. });
  35. server.listen(8888);

处理查询参数

使用url.parse
The url.parse() method takes a URL string, parses it, and returns a URL object.

  1. import * as url from "url";
  2. server.on("request", (request: IncomingMessage, response: ServerResponse) => {
  3. const { method, url: path, headers } = request;
  4. const { pathname, search } = url.parse(path);
  5. })
  6. 这样就可以得到path的所有信息
  7. 可以打印出来看看
  8. console.log(url.parse(path))

image.png

匹配任意文件

  1. const server = http.createServer();
  2. const publicDir = p.resolve(__dirname, "public");
  3. // path.resolve() 传入路径或路径片段,解析为绝对路径
  4. server.on("request", (request: IncomingMessage, response: ServerResponse) => {
  5. const { method, url: path, headers } = request;
  6. const { pathname, search } = url.parse(path);
  7. console.log(url.parse(path));
  8. // response.setHeader("Content-Type", "text/html; chartset=utf-8");
  9. // /index.html => index.html
  10. const fileName = pathname.substr(1);
  11. fs.readFile(p.resolve(publicDir, fileName), (error, data) => {
  12. if (error) {
  13. response.statusCode = 404;
  14. response.end(`你要的文件不存在`);
  15. } else {
  16. response.end(data.toString());
  17. }
  18. });
  19. });
  20. server.listen(8888);

将之前的switch修改,封装成一个方法,在publicDir目录下的文件都能匹配到。
fileName是去掉pathname前面的‘/’得到的

处理非GET请求以及文件缓存

处理非GET请求,因为是静态服务器,所以直接给他返回个405就好

  1. if (method !== "GET") {
  2. response.statusCode = 405;
  3. response.end();
  4. return;
  5. }

文件缓存通过设置响应头的Cache-Control属性实现

  1. response.setHeader("Cache-Control", `public, max-age=${catchAge}`);
  2. response.end(data.toString());

完整代码

  1. import * as http from "http";
  2. import { IncomingMessage, ServerResponse } from "http";
  3. import * as fs from "fs";
  4. import * as p from "path";
  5. import * as url from "url";
  6. // __dirname 表示当前文件所在目录
  7. const server = http.createServer();
  8. const publicDir = p.resolve(__dirname, "public");
  9. // path.resolve() 传入路径或路径片段,解析为绝对路径
  10. let catchAge = 3600 * 24 * 365;
  11. server.on("request", (request: IncomingMessage, response: ServerResponse) => {
  12. const { method, url: path, headers } = request;
  13. const { pathname, search } = url.parse(path);
  14. // 处理非GET请求,因为是静态服务器,直接不允许访问了
  15. if (method !== "GET") {
  16. response.statusCode = 405;
  17. response.end();
  18. return;
  19. }
  20. // response.setHeader("Content-Type", "text/html; chartset=utf-8");
  21. // /index.html => index.html
  22. let fileName = pathname.substr(1);
  23. if (fileName === "") {
  24. fileName = "index.html";
  25. }
  26. fs.readFile(p.resolve(publicDir, fileName), (error, data) => {
  27. if (error) {
  28. if (error.errno === -4058) {
  29. response.statusCode = 404;
  30. fs.readFile(p.resolve(publicDir, "404.html"), (error, data) => {
  31. response.end(data);
  32. });
  33. response.end(`你要的文件不存在`);
  34. } else if (error.errno === -4068) {
  35. response.statusCode = 403;
  36. response.end("无权查看目录内容");
  37. } else {
  38. response.statusCode = 500;
  39. response.end(`服务器繁忙,请稍后重试`);
  40. }
  41. } else {
  42. // 返回文件内容
  43. // 缓存文件内容 + 时长:31536000s
  44. // 浏览器是不会帮你缓存首页的,即使你设置了缓存
  45. response.setHeader("Cache-Control", `public, max-age=${catchAge}`);
  46. response.end(data.toString());
  47. }
  48. });
  49. });
  50. server.listen(8888);