nodejs 做动态网页

版权声明:版权有就有吧。 https://blog.csdn.net/m0_38044453/article/details/83788607
var http = require('http');
var url = require('url');
var router = require('./router');
// 创建服务端口为8000
http.createServer(function(request,response){
	
	// 判断是否为第二次访问
	if(request.url!=='/favicon.ico'){
		var pathname = url.parse(request.url).pathname;
		// 去除路径中的 /
		pathname = pathname.replace(/\//,'');
		try{
			router[pathname](request,response);
		}catch(err){
			console.log(err);
			response.writeHead(200,{'Contet-Type':'text/html;charset=utf-8'});
			response.write(err.toString());
			response.end('');
		}
	}
}).listen(8000);
console.log('Server is running in port 8000');

var url = require('url');
var readhtml = require('./readhtml');

var querystring = require('querystring');
function getRecall(req,res){
	res.writeHead(200,{'Contet-Type':'text/html;charset=utf-8'});
	function recall(data){
		res.write(data);
		res.end('');
	}
	return recall;
}

module.exports={
	login:function(req,res){
		// post 提交方式
		// 定义一个post变量,用于暂时储存请求体的信息
		var post = '';
		// 通过req的data事件监听函数,每当接受到请求体的数据,就到post变量中
		req.on('data',function(chunk){
			post +=chunk;			
		});
		// 注意异步
		// 在end事件触发后,通过querystring.parse将post街恶习为真正的post格式,然后向客户端返回
		req.on('end',function(){
			post = querystring.parse(post);
			//console.log('接受的参数'+ post['email']+'\n'+post['pwd']);
			

			arr = ['email','pwd'];

			function recall(data){
				dataStr = data.toString();
				for(var i =0;i<arr.length;i++){
					re = new RegExp('{'+arr[i]+'}','g');
					dataStr = dataStr.replace(re,post[arr[i]]);
				}

				res.write(dataStr);
				res.end('');

			}
			console.log('email===='+post['email']
				);
			console.log('password='+post['pwd']);
			
			// 接受到所有参数再显示页面
			readhtml.login('./login.html',recall);
			
		});	
	},
	showimg:function(req,res){
		res.writeHead(200,{'Contet-Type':'text/html;charset=utf-8'});
		readhtml.showimg('./1.jpg',res);
	}
}
var fs = require('fs');

module.exports = {
	login:function(path,recall){
		fs.readFile(path,function(err,data){
			if(err){
				console.log(err)
				recall('文件不存在。') // 返回异常信息
			}else{
				recall(data);
			}	
		});
	},
	showimg:function(path,res){
		fs.readFile(path,'binary',function(err,filedata){
			if(err){
				console.log(err);
				return;
			}else{
			
				res.write(filedata,'binary');
				res.end('');
			}
		});
	}
}
<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
  登录界面
  <img src='./showimg'/>
	<h1>email是:{email}</h1>	
	<h1>密码是:{pwd}</h1>
  <form action='./login' method='post'>
	<table align='center'>
		<tr>
			<td>email:</td>
			<td><input type='text' name='email' /></td>
		</tr>
		<tr>
			<td>密码:</td>
			<td><input type='password' name='pwd'/></td>
		</tr>
		<tr>
			<td align='center'><input type='submit' value='登录'/></td>
		</tr>
	</table>
  
  </form>
 </body>
</html>

主要就是,在往页面传数据的时候,根据正则表达式,把对应的字段替换掉。

猜你喜欢

转载自blog.csdn.net/m0_38044453/article/details/83788607
今日推荐