nodejs template engine

Homemade replace template

template.js  

the require FS = var ( 'FS') 
var = the require HTTP ( 'HTTP') 


var = http.createServer Server () 
var wwwDir = 'D: / vuejs / NodeJS / DAY2' 

server.on ( 'Request', function (REQ , RES) { 
	var URL = req.url 
	

	fs.readFile ( './ template.html', function (ERR, Data) { 
		IF (ERR) { 
			return res.end ( '404 Not Fount !!!!') 
		} 

		// 1. get all file names and directory names of the path 
			//fs.readdir ( 'path', function (ERR, Data) {}) 
		// 2. the resulting file and directory names in the alternative to template.html 
			// 2.1 template.html required alternate location reserved for special marker 
			//2.2 files generated Html content according 
		fs.readdir (wwwDir, function (error, Files) { 
			IF (error) { 
				return res.end ( 'Can Not Find WWW the dir. ') 
			} 
			Were content = ''
			files.forEach(function (item) {
				 content += `
				 	<tr>
						<td>${item}</td>
						<td>删 | 改</td>
					</tr>
				 `
			})
			data = data.toString()
			//console.log(data.replace( 'replacestring', content ));
			data = data.replace( 'replacestring', content )
			res.end( data )
		})

		
	})
})


server.listen(3000, function () {
	console.log('runing.....')
})

  

template.html

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>213</title>
</head>
<body>
	<table>
		<tr>
			<td>文件名</td>
			<td>操作</td>
		</tr>
		replacestring
	</table>
</body>
</html>

  

 

Guess you like

Origin www.cnblogs.com/jasonLiu2018/p/11184401.html