node初级入门之搭建http服务器

前置知识:

了解IP地址和端口是什么

http请求的大致过程(这个建议买本《图解http》看看)

http核心模块的使用

  1. 导入http模块
  2. 定义服务器程序端口
  3. 创建服务器对象
  4. 调用服务器的监听方法,让服务器监听浏览器需求

 

// 1、导入http模块
const http = require("http");

// 2、定义服务器程序端口
const port = 8080;// 端口号:1-65535 (有些服务已经有一些默认端口 apache nginx 80 web 服务。 MySQL:3306 MongoDB:27017)
// 注意:一个端口只能被一个服务进行使用,如果这个端口被某个服务使用,其他的服务不能在使用该端口的。这个时候出现端口冲突。如何解决?答:换个端口
// 建议:1-1024 端口(有些系统服务会使用这个范围的端口),不建议程序员自己使用。一般都使用 1024 以后的端口。

// 3、创建服务器对象
const server = http.createServer((request, response)=>{
    response.write("hello nodejs");  // 书写响应体内容
    response.end()   //发生响应到浏览器  当我们修改代码后,需要重新执行该文件,重启服务
});

// 4、调用服务器的监听方法,让服务器监听浏览器请求
server.listen(port,(error)=>{
    console.log(error);
    console.log(`server is running at port ${port}`);
});

//报错 address already in use :::8080  端口被占用

//如果需要解决中文乱码,需遵循http协议:
response.setHeader("Content-type","text/html;charset=utf-8");

获取请求的一些信息

const url = require("url");

const server = http.createServer((request, response)=>{
    console.log("------------------------------");
    let requestUrl = request.url;  // 获取本次请求的资源路径
    console.log(requestUrl);

    let method = request.method;  // 获取本次请求的方式
    console.log(method);

    let obj = url.parse( requestUrl, true);
    console.log(obj.query);    // 获取get请求的查询字符串    
    //  localhost:8080?name=nodejs&age=11   get请求

    //当存在 post 提交数据 data 事件立马执行,postData就是提交过来的数据对象
    request.on('data',(postData) => {   // 获取post请求的请求参数
        console.log(postData.toString());
    });

    response.write("hello nodejs");  // 书写响应体内容
    response.end()   //发生响应到浏览器  当我们修改代码后,需要重新执行该文件,重启服务
});

 写个Web服务器

注意看url地址

 

 

 

项目架构

搭建一个http服务器.js

const http = require("http");
const fs = require("fs");
const path = require("path");
//配置服务器程序的端口号
const port = 8081;

const server = http.createServer((request, response) => {
  //每来一个请求就执行一次这里的代码

  //判断浏览器需要哪个资源文件
  let reqUrl = request.url;
  if (reqUrl === "/") {
    //读取页面内容 返回信息
    let filePath = path.join(__dirname, "assets", "html", "index.html");
    let content = fs.readFileSync(filePath);
    response.end(content);
  } else if (reqUrl === "/detail.html" || reqUrl === "/index.html") {
    //读取页面内容 返回信息
    let filePath = path.join(__dirname, "assets", "html", "detail.html");
    let content = fs.readFileSync(filePath);
    response.end(content);
  } else {
    response.setHeader("Content-type", "text/html;charset=utf-8");
    response.end("404错误:该资源找不到!");
  }

  //遵循http协议,设置响应头信息
  // response.setHeader("Content-type", "text/html;charset=utf-8");
});

server.listen(port, (error) => {
  console.log(`WebServer is listening at port ${port} !`);
});

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>首页标题</h1>
    <hr />
    <p>欢迎来到我们网站!</p>
  </body>
</html>

 detail.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>详情页标题</h1>
    <hr />
    <p>详情页的内容</p>
    <p>详情页的内容</p>
    <p>详情页的内容</p>
  </body>
</html>

 

Guess you like

Origin blog.csdn.net/nxcniuxiangchen/article/details/121758684