Giter Club home page Giter Club logo

nodehttpserver's Introduction

nodeHttpServer

使用node原生http模块搭建服务器,实现对路由的解析以及请求的派发,实现静态资源和favicon资源的获取。主要用到的是node的http和fs模块的api,学习练手 运行项目,主文件为server.js 在npm命令行中输入npm start,或者node server.js启动项目

eventEmitter发送和接收事件

pulser.js

node中很多类都是eventEmitter的子类,如HTTPServer和HTTPClient类。eventEmitter定义在node的event模块中。on方法定义事件的监听器函数,emit方法触发事件。

服务器端监听http请求

httpsniffer.js和hwserver.js当发起一个请求时,在服务器端会输出请求相关日志

基本的web服务器

实现目标: 请求路由的选择、url对象解析、提取主机头信息、支持favicon请求、支持静态文件请求

basicServer.js

htserver 是一个http服务器对象,进行url对象的解析并存储在req的basicServer中,处理消息头,分派给相应的处理函数 htserver的basicServer存储请求的主机名、路径以及处理模块 htserver的addContainer检测模块是否存在,不存在则引入模块及配置 htserver的useFavicon和docRoot引入静态资源和favicon资源处理模块 lookupContainer:根据host和path匹配请求处理模块 dispatchToContainer:进行请求派发,调用相应模块的handle方法进行请求处理。

faviconHandler.js

staticHandler.js

nodehttpserver's People

Contributors

bramble-river avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.