工具

  • node-dev
  • ts-node
  • ts-node-dev

$ yarn global add ts-node-dev

新建项目

  • yarn init -y
  • 新建 index.ts
  • 使用命令行或WebStorm启动
  • $ yarn add --dev @types/node安装node声明文件
  • 引入http模块
  • 用http创建server
  • 监听server的request事件(可简写)
  • server.listen(8888) 开始监听8888端口
  • 使用 curl -v http://localhost:8888 发请求

    根据url返回不同的文件

    ```typescript import as http from ‘http’; import { IncomingMessage, ServerResponse } from ‘http’; import as fs from ‘fs’; import * as p from ‘path’ const server = http.createServer() const publicDir = p.resolve(__dirname, ‘public’) server.on(‘request’, (request: IncomingMessage, response: ServerResponse) => { const { method, url, headers } = request

    switch (url) {

    1. case '/index.html':
    2. response.setHeader('Content-Type', 'text/html;charset=utf-8')
    3. fs.readFile(p.resolve(publicDir, 'index.html'), (error, data) => {
    4. if (error) throw error;
    5. response.end(data.toString())
    6. })
    7. break;
    8. case '/style.css':
    9. response.setHeader('Content-Type', 'text/css;charset=utf-8')
    10. fs.readFile(p.resolve(publicDir, 'style.css'), (error, data) => {
    11. if (error) throw error;
    12. response.end(data.toString())
    13. })
    14. break;
    15. case '/main.js':
    16. response.setHeader('Content-Type', 'text/javascript;charset=utf-8')
    17. fs.readFile(p.resolve(publicDir, 'main.js'), (error, data) => {
    18. if (error) throw error;
    19. response.end(data.toString())
    20. })
    21. break;
    22. default:
    23. response.end('no file')
    24. break;

    } })

server.listen(8888)

  1. <a name="rDu4q"></a>
  2. # 处理查询参数
  3. **_url.parse_**
  4. ```typescript
  5. import * as http from 'http';
  6. import { IncomingMessage, ServerResponse } from 'http';
  7. import * as fs from 'fs';
  8. import * as p from 'path';
  9. import * as url from 'url';
  10. const server = http.createServer()
  11. const publicDir = p.resolve(__dirname, 'public')
  12. server.on('request', (request: IncomingMessage, response: ServerResponse) => {
  13. const { method, url: path, headers } = request
  14. const { pathname, search } = url.parse(path)
  15. console.log(pathname, search)
  16. switch (pathname) {
  17. case '/index.html':
  18. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  19. fs.readFile(p.resolve(publicDir, 'index.html'), (error, data) => {
  20. if (error) throw error;
  21. response.end(data.toString())
  22. })
  23. break;
  24. case '/style.css':
  25. response.setHeader('Content-Type', 'text/css;charset=utf-8')
  26. fs.readFile(p.resolve(publicDir, 'style.css'), (error, data) => {
  27. if (error) throw error;
  28. response.end(data.toString())
  29. })
  30. break;
  31. case '/main.js':
  32. response.setHeader('Content-Type', 'text/javascript;charset=utf-8')
  33. fs.readFile(p.resolve(publicDir, 'main.js'), (error, data) => {
  34. if (error) throw error;
  35. response.end(data.toString())
  36. })
  37. break;
  38. default:
  39. response.end('no file')
  40. break;
  41. }
  42. })
  43. server.listen(8888)

匹配任意文件

  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. const server = http.createServer()
  7. const publicDir = p.resolve(__dirname, 'public')
  8. server.on('request', (request: IncomingMessage, response: ServerResponse) => {
  9. const { method, url: path, headers } = request
  10. const { pathname, search } = url.parse(path)
  11. console.log(pathname, search)
  12. // response.setHeader('Content-Type', 'text/html;charset=utf-8')
  13. const filename = pathname.substr(1)
  14. fs.readFile(p.resolve(publicDir, filename), (error, data) => {
  15. if (error) {
  16. response.statusCode = 404
  17. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  18. response.end('你要的文件不存在')
  19. } else {
  20. response.end(data)
  21. }
  22. })
  23. })
  24. server.listen(8888)

处理不存在的文件

返回一个404页面

  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. const server = http.createServer()
  7. const publicDir = p.resolve(__dirname, 'public')
  8. server.on('request', (request: IncomingMessage, response: ServerResponse) => {
  9. const { method, url: path, headers } = request
  10. const { pathname, search } = url.parse(path)
  11. console.log(pathname, search)
  12. // response.setHeader('Content-Type', 'text/html;charset=utf-8')
  13. let filename = pathname.substr(1)
  14. if (filename === '') { filename = 'index.html' }
  15. fs.readFile(p.resolve(publicDir, filename), (error, data) => {
  16. if (error) {
  17. if (error.errno === -4058) {
  18. response.statusCode = 404
  19. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  20. fs.readFile(p.resolve(publicDir, '404.html'), (error, data) => {
  21. response.end(data)
  22. })
  23. } else if (error.errno === -4068) {
  24. response.statusCode = 403
  25. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  26. response.end('无权限访问目录')
  27. } else {
  28. response.statusCode = 500
  29. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  30. response.end('服务器有问题')
  31. }
  32. } else {
  33. response.end(data)
  34. }
  35. })
  36. })
  37. server.listen(8888)

处理非GET请求

405 Method Not Allowed

  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. const server = http.createServer()
  7. const publicDir = p.resolve(__dirname, 'public')
  8. server.on('request', (request: IncomingMessage, response: ServerResponse) => {
  9. const { method, url: path, headers } = request
  10. if (method !== 'GET') {
  11. response.statusCode = 405
  12. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  13. response.end('不支持非GET请求')
  14. return
  15. }
  16. const { pathname, search } = url.parse(path)
  17. console.log(pathname, search)
  18. // response.setHeader('Content-Type', 'text/html;charset=utf-8')
  19. let filename = pathname.substr(1)
  20. if (filename === '') { filename = 'index.html' }
  21. fs.readFile(p.resolve(publicDir, filename), (error, data) => {
  22. if (error) {
  23. if (error.errno === -4058) {
  24. response.statusCode = 404
  25. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  26. fs.readFile(p.resolve(publicDir, '404.html'), (error, data) => {
  27. response.end(data)
  28. })
  29. } else if (error.errno === -4068) {
  30. response.statusCode = 403
  31. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  32. response.end('无权限访问目录')
  33. } else {
  34. response.statusCode = 500
  35. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  36. response.end('服务器有问题')
  37. }
  38. } else {
  39. response.end(data)
  40. }
  41. })
  42. })
  43. server.listen(8888)

添加缓存选项

Catch-Control

  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. const server = http.createServer()
  7. const publicDir = p.resolve(__dirname, 'public')
  8. server.on('request', (request: IncomingMessage, response: ServerResponse) => {
  9. const { method, url: path, headers } = request
  10. if (method !== 'GET') {
  11. response.statusCode = 405
  12. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  13. response.end('不支持非GET请求')
  14. return
  15. }
  16. const { pathname, search } = url.parse(path)
  17. console.log(pathname, search)
  18. // response.setHeader('Content-Type', 'text/html;charset=utf-8')
  19. let filename = pathname.substr(1)
  20. if (filename === '') { filename = 'index.html' }
  21. fs.readFile(p.resolve(publicDir, filename), (error, data) => {
  22. if (error) {
  23. if (error.errno === -4058) {
  24. response.statusCode = 404
  25. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  26. fs.readFile(p.resolve(publicDir, '404.html'), (error, data) => {
  27. response.end(data)
  28. })
  29. } else if (error.errno === -4068) {
  30. response.statusCode = 403
  31. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  32. response.end('无权限访问目录')
  33. } else {
  34. response.statusCode = 500
  35. response.setHeader('Content-Type', 'text/html;charset=utf-8')
  36. response.end('服务器有问题')
  37. }
  38. } else {
  39. //返回文件内容
  40. response.setHeader('Cache-Control', 'public,max-age=30000000')
  41. response.end(data)
  42. }
  43. })
  44. })
  45. server.listen(8888)

响应内容启用gzip

zlib