美文网首页我爱编程
node.js web 模块

node.js web 模块

作者: 飞鱼_JS | 来源:发表于2018-03-30 09:23 被阅读0次

什么是 Web 服务器?

Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服务器的基本功能就是提供Web信息浏览服务。它只需支持HTTP协议、HTML文档格式及URL,与客户端的网络浏览器配合。

大多数 web 服务器都支持服务端的脚本语言(php、python、ruby)等,并通过脚本语言从数据库获取数据,将结果返回给客户端浏览器。

目前最主流的三个Web服务器是Apache、Nginx、IIS、node。

Web 应用架构

image.png
  • Client - 客户端,一般指浏览器,浏览器可以通过 HTTP 协议向服务器请求数据。
  • Server - 服务端,一般指 Web 服务器,可以接收客户端请求,并向客户端发送响应数据。(node服务器)
  • Business - 业务层, 通过 Web 服务器处理应用程序,如与数据库交互,逻辑运算,调用外部程序等。(node.js 脚本)
    Node.js 提供了 http 模块,http 模块主要用于搭建 HTTP 服务端和客户端,使用 HTTP 服务器或客户端功能必须调用 http 模块,
  • Data - 数据层,一般由数据库组成。

node 创建服务端

const http =require('http');
const fs = require('fs');
const url = require('url');
http.createServer((req,res)=>{
    let pathname = url.parse(req.url).pathname;
    console.log('requset for'+pathname+'received');
    fs.readFile(pathname.substr(1),(err,data)=>{
        if(err){
            console.log("readFile err",err)
            res.writeHead(404,{'Content-Type':'text/html;charset=utf-8'});
        }else{
            res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'})
            res.write(data.toString())
        }
        res.end()
    })
}).listen(8000);
console.log("Server is running at 127.0.0.1:8080");

node 创建客户端

var http = require('http');
 
// 用于请求的选项
var options = {
   host: 'localhost',
   port: '8080',
   path: '/index.html'  
};
 
// 处理响应的回调函数
var callback = function(response){
   // 不断更新数据
   var body = '';
   response.on('data', function(data) {
      body += data;
   });
   
   response.on('end', function() {
      // 数据接收完成
      console.log(body);
   });
}
// 向服务端发送请求
var req = http.request(options, callback);
req.end();

相关文章

  • http模块

    什么是http模块 http模块是Node.js官方提供的、用来创建web服务器的模块。通过 http模块提供的h...

  • nodejs的http

    http是node.js官方提供的、用来创建web服务器的模块,通过http模块提供的http。createSer...

  • node.js web 模块

    什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服务器的基本...

  • Node.js Web 模块

    Node.js 提供了 http 模块,http 模块主要用于搭建 HTTP 服务端和客户端,使用 HTTP 服务...

  • Node.js Web 模块

    使用 Node 创建 Web 服务器 var http = require('http');var fs = re...

  • Node学习(11)--web模块

    Node.js Web 模块 什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计...

  • 关于node.js一些模块的记录「FS模块」

    目录 Node.JS教程 FS模块 Path模块 FS模块 Path模块 Node.js path 模块提供了一些...

  • Node.js - Web相关

    针对Node.js的特性,当然最主要的应用也就是Web开发了;上一篇我们了解到Node.js中包含了一些核心模块,...

  • Npm 常用指令 个人总结 - wanna

    淘宝镜像 Node.js web框架模块 express 1.本地安装将安装包放在 /usr/local 下或者你...

  • webpack-dev-server参数配置

    自从学习了node.js 的http 模块,我们就能想到 webpack-dev-server实质上是启动了web...

网友评论

    本文标题:node.js web 模块

    本文链接:https://www.haomeiwen.com/subject/fiybcftx.html