nodejs之静态文件托管、 路 由、EJS 模板引擎、GET、POST

1、静态文件托管

  静态文件托管:是指对于一个js方法进行封装,提高代码可读性

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

    //获取文件类型的方法  私有
    function getMime(extname,callback){  /*获取后缀名的方法*/

        fs.readFile('./mime.json',function(err,data){

        if(err){
            console.log('mime.json文件不存在');
            return false;
        }
        //console.log(data.toString());

        var Mimes=JSON.parse(data.toString());

        var result= Mimes[extname] || 'text/html';

        callback(result)

        })


    }

    exports.statics=function(req,res,staticpath){


        var pathname=url.parse(req.url).pathname;   /*获取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(staticpath+'/'+pathname,function(err,data){

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

            console.log('404');

            fs.readFile(staticpath+'/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{ /*返回这个文件*/

               getMime(extname,function(mime){
                res.writeHead(200,{"Content-Type":""+mime+";charset='utf-8'"});
                res.write(data);
                res.end(); /*结束响应*/
            });

            }
        })

        }

    }
View Code

2、路由
  路由:是后台管理页面的跳转

   var http = require('http');
    var url = require('url');

    http.createServer(function (req,res) {
        var  pathname = url.parse(req.url).pathname;
        if(pathname=='/login'){
        res.end('login');
        }else  if(pathname=='/register'){
        res.end('register');
        }else if(pathname=='/order'){
        res.end('order');
        }else {
        res.end('index');
        }
    }).listen('8001');

3、EJS 模块引擎   1、安装ejs

        npm install ejs
    2、引入ejs模块
        var ejs = require('ejs');
    3、调用ejs.renderFile,
        if(pathname=='/login'){
        var data='我是后台数据';
        var list=['1','2','3'];
        ejs.renderFile('EJS/views/login.ejs', //跳转ejs渲染界面
                {msg:data,list:list}, //后台传入数据到前端ejs界面
                function (err,data) { 
            if(err){
            console.log(err);
            return false;
            }
            console.log("hha");
            res.end(data);
        })
        }
    4、ejs前端界面获取后台数据
        <h2><%=msg%></h2>
        <ul>
            <%
            for(var i =0 ;i<list.length;i++){
                %>
                <li><%=list[i]%></li>
                <%
            }
            %>
        </ul>

5、EJS 常用标签

      1、<% %>流程控制标签
      2、<%= %>输出标签(原文输出 HTML 标签)
      3、<%- %>输出标签(HTML 会被浏览器解析)

具体的ejs操作,请查阅:https://www.npmjs.com/package/ejs

4、后台获取get,post请求数据

   1、获取请求方式
        var method=req.method.toLowerCase();
    2get请求,拿去其数据
         var params =url.parse(req.url,true).query;
         console.log(params);
    3post请求,拿取其数据,通过事件触发机制
        var postStr = "";
        req.on('data',function (chunk) {//监听post请求,获取数据
            postStr += chunk;
        })
        req.on('end',function (err,chunk) { //一块块读取到数据后,将数据存储起来
            fs.appendFile('login.txt',postStr+'
',function (err) {//将登陆信息写入文件,可以改成mysql,存入数据库
            if(err){
                console.log(err);
                return ;
            }
            console.log("写入文件成功!");
            })
            res.end("<script>alert('登录成功'); history.back()</script>");//返回登陆成功,并跳转到登陆界面
            res.end('dologin--post->'+postStr);
         }
原文地址:https://www.cnblogs.com/ywjfx/p/10396655.html