zoukankan      html  css  js  c++  java
  • [Nodejs]十分钟快速编写简单静态文件服务器

    学了几天Nodejs 后我又干上了前端的活。这次遇到的问题是,我想在不同的设备上方便的查看我编写的网页,很自然的就想到要是能在本地搭建一个简单的http服务器的话,那局域网内的所有设备都可以访问了,这可是极好的。于是趁热马上动手实现一个简单服务器。

    需求:访问http://192.168.1.100/index.html 可以返回指定网页及其涉及的资源(css,js,图片文件等)

    function start(route, handle) {
        http.createServer(function(request, response) {
    
            request.addListener("end", function(){
    
                fetchFile(request, response)
            })
    
        }).listen(8888);
    }

    调用createServer 方法创建一个服务器并监听8888端口,在回调内监听end 事件,并把抓取文件并返回的工作交给即将尚未编写的fetchFile 函数。

    var typeMap = {
        ".png" : "image/png",
        ".jpg" : "image/jpeg",
        ".js"  : "application/javascript",
        ".css" : "text/css",
        ".html" : "text/html"
    }

    接下来,在编写fetchFile 之前,我们需要一个 “后缀名->Conent-Type” 的表,这样在浏览器访问指定资源的时候,我们能返回响应的Content-Type。

    var fs = require('fs')
    function fetchFile(req, res) {
        var url = req.url
        fs.readFile("."+req.url, function(err, data) {
            var contentType = "text/plain"
            if (!err) {
                // find type of file
                for (key in typeMap) {
                    if (req.url.match(key)) {
                        contentType = typeMap[key]
                        break
                    }
                }
                res.writeHead(200, {"Content-Type": contentType})
                res.write(data)
                res.end()
            }
            else {
                res.writeHead(404, {"Content-Type":contentType})
                res.end()
            }
        })
    }

    最后一步,当我们收到一个请求的时候,只需把请求的url 拿出来,在typeMap 里找出响应的Content-Type,之后调用fs 模块的异步文件读取方法,

    • 如果文件存在,我们就返回正确的头信息,附上文件内容,发送响应。
    • 如果文件不存在,返回404状态码,结束响应。

    最后不要忘记调用我们的start 方法。

    完整代码:

    var http = require("http");
    
    function start(route, handle) {
        http.createServer(function(request, response) {
    
            request.addListener("end", function(){
                fetchFile(request, response)
            })
    
        }).listen(8888);
    }
    
    var typeMap = {
        ".png" : "image/png",
        ".jpg" : "image/jpeg",
        ".js"  : "application/javascript",
        ".css" : "text/css",
        ".html" : "text/html"
    }
    
    var fs = require('fs')
    function fetchFile(req, res) {
        var url = req.url
        fs.readFile("."+req.url, function(err, data) {
            var contentType = "text/plain"
            if (!err) {
                // find type of file
                for (key in typeMap) {
                    if (req.url.match(key)) {
                        contentType = typeMap[key]
                        break
                    }
                }
                res.writeHead(200, {"Content-Type": contentType})
                res.write(data)
                res.end()
            }
            else {
                res.writeHead(400, {"Content-Type":contentType})
                res.end()
            }
        })
    }
    
    start()
    server.js
  • 相关阅读:
    基于HttpListener的web服务器
    基于TcpListener的web服务器
    一个简单的web服务器
    c# 6.0新特性(二)
    c# 6.0新特性(一)
    c#之Redis实践list,hashtable
    html5摇一摇[转]
    在Microsoft-IIS/10.0上面部署mvc站点的时候,出现404的错误
    [实战]MVC5+EF6+MySql企业网盘实战(28)——其他列表
    让DELPHI自带的richedit控件显示图片
  • 原文地址:https://www.cnblogs.com/agentgamer/p/4378097.html
Copyright © 2011-2022 走看看