4.4-node-web

待我称王封你为后i 2023-07-22 09:14 100阅读 0赞

1 模块查找机制

  1. require('./a'); // a.js
1.1 模块有路径没有后缀名

1 首先找同名js 找到则执行

2 找不到则找同名js文件夹

3 假如找到同名js文件夹,会找package.json文件main选项指定的入口文件

4 假如package.json文件main选项指定的入口文件不存在或者没配置,则会

找有没有index.js

5 没有index.js会报错

  1. require('a'); // require('fs') require('mime')
1.2没有路径也没有后缀

1 首先会假设这是系统模块

2 node会去node_modules文件夹

3 首先看有没有该名字的js

4 再看有没有该名字的文件夹

5 假如找到同名js文件夹,会找package.json文件main选项指定的入口文件

6 假如package.json文件main选项指定的入口文件不存在或者没配置,则会

找有没有index.js

7 没有index.js会报错

node-web

客户端

服务器端 处理数据和业务逻辑

​ 请求

客户端 ——————> 服务器端

​ <——————

​ 响应

ip地址/域名 http://www.baidu.com

端口 如: 9999

URL http://www.baidu.com

https://www.baidu.com/index.html

本地ip 127.0.0.1 本地域名localhost

报文 请求和响应的过程中传递的数据块

响应报文

​ 1 HTTP状态码

​ 200 OK 请求成功

​ 404 请求资源不存在

​ 500 服务器错误

​ 400 客户端请求有语法错误

​ 2 内容类型

​ text/html

​ text/css

​ text/javascript

​ image/jepg

​ application/json

怎样创建一个服务器

  1. // http 创建服务器模块
  2. const http = require('http');
  3. /* 创建一个服务器*/
  4. const app = http.createServer();
  5. // 监听客户端请求
  6. app.on('request', (req, res) => {
  7. console.log(req);
  8. // 请求报文
  9. console.log(req.headers);
  10. // 请求地址
  11. console.log('请求地址', req.url) // /favicon.ico
  12. // 请求方法
  13. console.log('请求方法', req.method); // GET
  14. // 响应 给客户端回写
  15. res.end('<h1>hello</h1>');
  16. });
  17. // 监听端口
  18. app.listen(9999);
  19. console.log('服务器已经启动,请访问localhost:9999');
  20. /* 方法二 const app = http.createServer((req, res) => { console.log(111); res.writeHead(200, { 'content-type': 'text/html;charset=utf8' }); res.end('<h1>hi,小鲜肉</h1>'); }); app.listen(6688); */

HTTP请求处理与响应处理

1 请求参数

get请求参数

​ http://localhost:9999/?username=admin&pwd=123456

​ 参数回访制在浏览器地址栏中,可以借用url模块parese处理

​ get请求 1 浏览器直接输入网址 2 link 3 script 4 img 5 form表单

  1. const http = require('http');
  2. // url模块,处理url地址
  3. const url = require('url');
  4. const app = http.createServer();
  5. app.on('request', (req, res) => {
  6. //console.log(req.url); // /?username=admin&pwd=123456
  7. // let urlStr = url.parse(req.url, true);
  8. // 第二个参数‘true’可以把参数解析成对象形式
  9. // 不写此参数时username=admin&pwd=123456
  10. // console.log(urlStr); //{ username: 'admin', pwd: '123456' }
  11. let { query } = url.parse(req.url, true);
  12. res.end(`${ query.username}-${ query.pwd}`);
  13. // res.end('welcome');
  14. })
  15. app.listen(9999);
  16. console.log('已经启动');
POST请求

​ 1 参数是被放在请求体中进行传输

​ 2 node处理POST请求,需要使用data和end事件

​ 3 使用querystring模块处理

  1. const http = require('http');
  2. const qs = require('querystring');
  3. const app = http.createServer();
  4. app.on('request', (req, res) => {
  5. //console.log(req.url); // 输出/
  6. let postData = '';
  7. // 监听,数据变化拼接
  8. req.on('data', (chunk) => {
  9. postData += chunk;
  10. });
  11. // 监听结束返回
  12. req.on('end', () => {
  13. console.log(postData); //username=admin&pwd=11111
  14. let { username, pwd } = qs.parse(postData); // {uername:**, pwd:**}
  15. console.log(username, pwd);
  16. });
  17. })
  18. app.listen(9999);
  19. console.log('已经启动');

其他前端界面

  1. <body>
  2. <!-- 默认method=get -->
  3. <!-- <form action="http://localhost:9999"> -->
  4. <!-- 用户名:<input type="text" name="username"> -->
  5. <!-- 密码:<input type="password" name="pwd"> -->
  6. <!-- <input type="submit" value="登陆"> -->
  7. <!-- 提交后地址变化=> http://localhost:9999/?username=admin&pwd=123456 -->
  8. <!-- </form> -->
  9. <!-- post请求 -->
  10. <form action="http://localhost:9999" method="POST">
  11. 用户名:<input type="text" name="username">
  12. 密码:<input type="password" name="pwd">
  13. <input type="submit" value="登陆">
  14. </form>
  15. </body>

在这里插入图片描述

路由 客户端请求地址与服务端程序代码的对应关系

静态资源: 服务器不需要处理,可以直接响应给客户端

动态资源: 相同的请求地址,不同的响应资源

​ /blog/article?id=1

​ /blog/article?id=2

发表评论

表情:
评论列表 (有 0 条评论,100人围观)

还没有评论,来说两句吧...

相关阅读

    相关 44、Map

    ![8e3889229aa7495db39451d2d61e5c01.png][] 一、基本介绍: 1、Map接口实现类的特点[很实用] 注意:这里讲的是JDK8的Map

    相关 44. 通配符匹配

    给定一个字符串 (s) 和一个字符模式 § ,实现一个支持 ‘?’ 和 ‘\’ 的通配符匹配。 ‘?’ 可以匹配任何单个字符。 ‘\’ 可以匹配任意字符串(包括空字符串)