stm32+w5500实现web服务_【NodeJS】简单静态WEB服务器实现

该博客详细介绍了如何使用STM32微控制器和W5500网络芯片实现一个简单的Web服务,同时结合NodeJS创建一个静态文件服务器。内容包括利用HTTP、URL、Path和Fs模块来搭建服务,逐步改进以动态获取文件类型,并提供了相关脚本如getmine.js、getmimefromfile.js以及mine.json,旨在提供全面的文件类型支持。
摘要由CSDN通过智能技术生成

34cb795df45a62f4995ff45ba30aa39e.gif

说明

利用HTTP模块 URl模块 Path模块 Fs模块创建 在项目文件夹下,创建文件夹static,下面创建index.html

//引入http模块
var http=require('http');
//fs模块
var fs=require('fs');
http.createServer(function(req,res){
    //http://localhost:8001/news.html /news.html
    //http://localhost:8001/index.html /index.html
    //css/dmb.bottom.css
    var pathname=req.url;
    if(pathname=='/'){
        pathname='/index.html'; /*默认加载的首页*/
    }
    if(pathname!='/favicon.ico'){ /*过滤请求favicon.ico*/
        console.log(pathname);
        //文件操作获取 static下面的index.html
        fs.readFile('static/'+pathname,function(err,data){
            if(err){ /*么有这个文件*/
                console.log('404');
            }else{ /*返回这个文件*/
                res.writeHead(200,{ "Content-Type":"text/html;charset='utf-8'"});
                res.write(data);
                res.end(); /*结束响应*/
            }
        })
    }
}).listen(8001);

改进,加入path模块,动态获取文件类型

//引入http模块
var http=require('http');
//fs模块
var fs=require('fs');
//path模块
var path=require('path'); /*nodejs自带的模块*/
var mimeModel=require('./model/getmime.js');
//console.log(mime.getMime('.css')); //获取文件类型
http.createServer(function(req,res){
    //http://localhost:8001/news.html /news.html
    //http://localhost:8001/index.html /index.html
    //css/dmb.bottom.css
    var pathname=req.url;
    if(pathname=='/'){
        pathname='/index.html'; /*默认加载的首页*/
    }

    //获取文件的后缀名
    var extname=path.extname(pathname);
    if(pathname!='/favicon.ico'){ /*过滤请求favicon.ico*/
        //console.log(pathname);
        //文件操作获取 static下面的index.html
        fs.readFile('static/'+pathname,function(err,data){
            if(err){ /*么有这个文件*/
                console.log('404');
                fs.readFile('static/404.html',function(error,data404){
                    if(error){
                        console.log(error);
                    }
                    res.writeHead(404,{ "Content-Type":"text/html;charset='utf-8'"});
                    res.write(data404);
                    res.end(); /*结束响应*/
                })

            }else{ /*返回这个文件*/

                var mime=mimeModel.getMime(extname); /*获取文件类型*/
                res.writeHead(200,{ "Content-Type":""+mime+";charset='utf-8'"});
                res.write(data);
                res.end(); /*结束响应*/
            }
        })
    }
}).listen(8001);

加入url模块

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

//fs模块

var fs=require('fs');

//path模块
var path=require('path'); /*nodejs自带的模块*/

//url模块

var url=require('url');


var mimeModel=require('./model/getmime.js');

//console.log(mime.getMime('.css')); //获取文件类型

http.createServer(function(req,res){



    //http://localhost:8001/news.html /news.html
    //http://localhost:8001/index.html /index.html

    //css/dmb.bottom.css

    //xxx.json?214214124

    var pathname=url.parse(req.url).pathname;

    console.log(pathname);

    if(pathname=='/'){
        pathname='/index.html'; /*默认加载的首页*/
    }

    //获取文件的后缀名
    var extname=path.extname(pathname);

    if(pathname!='/favicon.ico'){ /*过滤请求favicon.ico*/
        //console.log(pathname);
        //文件操作获取 static下面的index.html

        fs.readFile('static/'+pathname,function(err,data){

            if(err){ /*么有这个文件*/

                console.log('404');

                fs.readFile('static/404.html',function(error,data404){
                    if(error){
                        console.log(error);
                    }
                    res.writeHead(404,{ "Content-Type":"text/html;charset='utf-8'"});
                    res.write(data404);
                    res.end(); /*结束响应*/
                })

            }else{ /*返回这个文件*/

                var mime=mimeModel.getMime(extname); /*获取文件类型*/
                res.writeHead(200,{ "Content-Type":""+mime+";charset='utf-8'"});
                res.write(data);
                res.end(); /*结束响应*/
            }
        })
    }

}).listen(8001);

getmine.js

exports.getMime=function(extname){ /*获取后缀名的方法*/

    switch (extname){

        case '.html':

            return 'text/html';
        case '.css':

            return 'text/css';

        case '.js':

            return 'text/javascript';

        default:
            return 'text/html';
    }

}

继续优化,使其文件类型的获取更加全面

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

//fs模块

var fs=require('fs');

//path模块
var path=require('path'); /*nodejs自带的模块*/

//url模块

var url=require('url');

//引入扩展名的方法是在文件里面获取到的。

var mimeModel=require('./model/getmimefromfile.js');

//console.log(mimeModel.getMime('.css')); //获取文件类型

http.createServer(function(req,res){



    //http://localhost:8001/news.html /news.html
    //http://localhost:8001/index.html /index.html

    //css/dmb.bottom.css

    //xxx.json?214214124

    var pathname=url.parse(req.url).pathname;

    console.log(pathname);

    if(pathname=='/'){
        pathname='/index.html'; /*默认加载的首页*/<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值