全栈工程师 04 笔记

node搭建web服务器(静态页)

一、引入 required 模块 创建服务器

使用 require 指令来载入 http 模块,并将实例化的 HTTP 赋值给变量 http,实例如下:

  const http = require('http');//引入 HTTP 模块

接下来使用 http.createServer() 方法创建服务器,并使用 listen 方法绑定 1333 端口。 函数通过 request, response 参数来接收和响应数据

  //声明一个ip
  const ip = '192.168.1.105';
  //声明一个端口
  const port = 1333;
  http.createServer(function (request, response) {

    // 发送 HTTP 头部
    // HTTP 状态值: 200 : OK
    // 内容类型: text/html
    response.writeHead(200, {'Content-Type': 'text/html'});

        // 发送响应数据 "Hello World"
        response.write('<p>Hello 全栈工程师</p>')
        response.end();

      }).listen(port,ip,()=>{

        // 终端打印如下信息
        console.log(`Server running at http://${ip}:${port}/`);

      });

二、封装函数、引入url模块

实例:
  const http = require('http');//引入http
  const url = require('url');//引入url

  const ip = '192.168.1.105';
  const port = 1333;

  var func = function (request, response) {
    var reqUrl = url.parse(request.url);
    console.log(reqUrl.pathname);
    // 发送 HTTP 头部
    // HTTP 状态值: 200 : OK
    // 内容类型: text/html
    response.writeHead(200, {'Content-Type': 'text/html'});
    // 发送响应数据 "Hello World"
    response.write('<p>Hello 全栈工程师</p>')
    response.end();
  }
  var serverFunc = function(){
    // 终端打印如下信息
    console.log(`Server running at http://${ip}:${port}/`);
  }
  http.createSerever(func).listen(port,ip.serverFunc);

三、引入文件系统(fs)、响应html静态文件

实例:
  const http = require('http');//引入http
  const url = require('url');//引入url
  const fs = require('fs');//fs

  const ip = '192.168.1.105';
  const port = 1333;

  var func = function (request, response) {
    var path = url.parse(request.url).pathname;//获取url文件路径
    console.log(path);
    //判断路径
    switch(path){
      case ''||'/'||'index':
        //打开文件fs.readFile
        fs.readFile('./index.html',function(err,content){

          //判断错误
          if(err){
            response.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
            response.write(err.message);
            response.end();
          }else{
            response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
            response.write(content.toString());
            response.end();
          }
        })
        break;
        default:
    }

  }
  var serverFunc = function(){
    // 终端打印如下信息
    console.log(`Server running at http://${ip}:${port}/`);
  }
  http.createSerever(func).listen(port,ip.serverFunc);

四、终端运行node.js脚本开启服务

  node app.js
  //Server running at http://192.168.23.128:1333

五、浏览器访问

  http://192.168.23.128:1333

学习总结

从头到尾搭建了node.js服务器,比php简单、有大量的API提供了便利,API很多,需要看的也很多,加油吧!
模块网址: www.npmjs.org

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,084评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,123评论 25 709
  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 11,524评论 0 6
  • 7个和尚住一起,每天共喝一桶粥,但每天都觉得不够。 一开始,他们抓阄约定谁分粥,结果,只有在轮到在分粥的那一天,才...
    我是多鸽阅读 2,498评论 0 0
  • 每个女孩,都是一个天使,是爸爸的小情人,妈妈的小棉袄,是上苍给父母最好的礼物。 而我呢——爸爸眼中有些多余的孩子,...
    笨笨的蜗牛爬呀爬阅读 3,308评论 0 4

友情链接更多精彩内容