Node.js - 服务器相关概念 - 核心模块path/http - response常用属性

服务器相关概念

1.服务器
  • 提供服务
  • 就是一台电脑
2.IP地址
  • ipv4
  • ipv6
  • 表示计算机中的唯一地址
  • 本机地址
    • 127.0.0.1
    • localhost
3.域名
  • ip地址的一个别名
  • 注册购买
4.http协议的作用
  • 方便调试代码
  • 规定客户端与服务端交互信息的约束
5.http协议报文
  • 请求报文 - 浏览器发送给服务器
请求行存放当前请求地址 , get或post请求
请求体存放 提交给后台的参数 , post 帐号密码
请求头token 登陆凭证 , 当前浏览器相关信息
  • 响应报文 - 服务器响应给浏览器
响应行http状态码是什么 , 200 成功.404找不到.500服务器出错
响应体响应客户端请求数据
响应头告诉浏览器当前的返回的内容的类型是什么

核心模块 path

  1. 处理路径
  2. path.join 拼接
  3. __dirname 返回当前文件的绝对路径

1.拼接路径

// path  核心模块 处理 路径问题
// D:\传智播客\广州前端68期\课程\node\01-Day\01-教学资料

const path = require("path");

// 自动帮我们拼接路径  
const url = path.join("data", "image", "2.json");
console.log(url);

2.path 获取文件绝对路径

// path 核心模块 处理路径问题

// 导入path核心模块
const path = require('path');
// 拼接路径
const url = path.join('data', 'images', '2.jpg');
// console.log(url);

// __dirname: 绝对路径
console.log(__dirname);
const myfile = path.join(__dirname, './01 - path获取文件绝对路径.js');
console.log(myfile);

3.开启一个服务器

// // 1.引入http核心模块
// const http = require('http');

// // 2.调用createSever方法创建一个服务
// // 通过返回值 来指定端口
// const app = http.createServer((request, response) => {
//     // request:当浏览器来访问我的服务器 request存放请求报文信息(行 头 体)
//     // response: 用来设置返回给浏览器的响应信息(行 头 体)
//     response.end('hello,byby');
// });

// // 3.开启指定端口
// app.listen(8001, () => {
//     console.log('这是我的服务器');
// });

// 1.引入http核心模块
const http = require('http');

// 2.创建一个服务
const app = http.createServer((req, res) => {
    res.end('我是你爸爸真伟大,养你这么大');
});

// 3.监听为8001的端口
app.listen(8001, () => {
    console.log('第二个服务器');
});

4-响应对象response常用属性

// 1.导入http核心模块
const http = require('http');

// 2.创建服务
const app = http.createServer((request, response) => {
    // 解决中文乱码
    response.setHeader('Content-Type', 'text/ plain;charset=utf-8');

    // 设置响应状态码
    response.statusCode = 200;

    // 响应数据的内容
    response.end('我是你爸爸真伟大');
});

// 3.监听端口
app.listen(8001, () => {
    console.log('8001  开启成功');
});

5.返回随机数

const http = require('http');

const app = http.createServer((request, response) => {
    console.log(response);

    if (request.url === 'random') {
        response.end(Math.random() + '');
    } else {
        response.statusCode = 404;
    }
});

app.listen(8001, () => {
    console.log('8001,开启');

6.服务器提供静态资源

const http = require('http');
const fs = require('fs');

// 创建一个服务器
const app = http.createServer((req, res) => {
    if (req.url === '/index.html') {
        // 响应头设置
        res.setHeader('Content-Type', '	text/html');
        // 同步读取文件
        let index = fs.readFileSync('./public/index.html', 'utf-8');
        res.end(index);
    } else if (req.url === '/chart.html') {
        res.setHeader('Content-Type', '	text/html');
        let chart = fs.readFileSync('./public/chart.html', 'utf8');
        res.end(chart);
    } else if (req.url === '/css/index.css') {
        res.setHeader('Content-Type', 'text/css');
        let css = fs.readFileSync('./css/index.css', 'utf8');
        res.end(css);
    } else if (req.url === '/js/index.js') {
        res.setHeader('Content-Type', 'application/x-javascript');
        let js = fs.readFileSync('./js/index.js', 'utf8');
        res.end(js);
    } else if (req.url === '/images/5f8de1ffa140a12d88f1131a64114897.png') {
        res.setHeader('Content-Type', '	application/x-img');
        let img = fs.readFileSync('./images/5f8de1ffa140a12d88f1131a64114897.png');
        res.end(img);
    } else {
        res.statusCode = 404;
        res.end('');
    }
});

app.listen(8001, () => {
    console.log('8001,开启');
});

优化代码

// 引入核心模块
const http = require('http');
const fs = require('fs');

// 创建一个服务器
const app = http.createServer((req, res) => {
    // 1 获取 客户端请求的 url
    const url = req.url; // url = "/index.html"  => /1ndex.html => 返回404
    console.log(url);

    // 2 根据 url 找寻找服务器上的对应的那个文件  try catch
    try {
        const file = fs.readFileSync('public' + url);
        // 3 将该文件 返回
        res.end(file);
    } catch (error) {
        // 4 文件找不到
        res.statusCode = 404;
        res.end('');
    }
});

app.listen(8001, () => {
    console.log('8001');
});

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Henry_ww

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值