node通过正则查找替换模板指定变量

regularMain.js代码:
var http = require('http');
var url=require('url');
var router=require('./router');//调用只有一个函数的js文件

http.createServer(function (request,response){
    response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
    if(request.url!="/favicon.ico"){ //清楚第二次访问
        var pathname=url.parse(request.url).pathname;
        response.write(pathname+"<br>");//结果类似/name=ceshi
        pathname=pathname.replace(/\//,'');//正则去掉前面的斜杠

        try{
            router[pathname](request,response);
        }catch(err){
            console.log(err);
            response.write(err.toString()+"<br>");
        }
    }
}).listen(8001);


login.html代码:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试提交参数</title>
</head>
<body>
邮箱地址是:{email}
密码是:{pwd}
    <form action="/login" method="post">
        <input type="text" name="email"><br>
        <input type="text" name="pwd"><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/abiao555/article/details/108166575