var fs = require('fs');
var http = require('http');
var template = require('art-template');
var Url = require('url');
const {
url } = require('inspector');
function GetNowFormatDate(){
var MyDate = new Date();
var year = MyDate.getFullYear();
var month = MyDate.getMonth();
var day = MyDate.getDate();
var h = MyDate.getHours();
var m = MyDate.getMinutes();
if ( m < 10 ) m = '0'+ m ;
var s = MyDate.getSeconds();
if( s < 10 ) s = '0' + s;
var NowTime = year + '/' + month + '/' + day + '/' + h + ':' + m;
return NowTime;
}
var content = [
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
}
];
http.createServer(function(request,response){
var url = request.url;
var urlObj = Url.parse(request.url,true);
var ObjPathName = urlObj.pathname;
if( ObjPathName === '/'){
fs.readFile('/web/Node/html/notemain.html',function(error,data){
if(error){
return response.end('Read Failure!');
}else {
return response.end(template.render(data.toString(),{
content: content
}));
}
});
}
else if ( ObjPathName.indexOf('/web/Node/') === 0 ){
fs.readFile(url , function(error,data){
if( error ){
return response.end('Read Failure!');
}else {
return response.end(data);
}
});
}else if(ObjPathName === '/web/post'){
fs.readFile('/web/Node/html/post.html',function(error,data){
if (error){
return response.end('Read Failure!');
}else {
return response.end(data);
}
});
}else if( ObjPathName === '/form'){
fs.readFile('/web/Node/html/notemain.html',function(error,data){
if(error){
return response.end('Read Failure!');
}else{
var example = {
};
example.name = urlObj.query.Name ;
example.passage = urlObj.query.comment;
example.dateTime = GetNowFormatDate();
content.unshift(example);
response.statusCode = 302;
response.setHeader('Location','/');
response.end();
}
});
}
else {
fs.readFile('/web/Node/html/404.html',function(error,data){
if(error){
return response.end('Read failure!');
}else {
return response.end(data);
}
});
}
})
.listen(5000,function(){
console.log('Server had been started , you can access from ip: http://127.0.0.1:5000');
});
notemain.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NoteMain</title>
<link rel="stylesheet" href="\web\Node\css\notemain.css" >
<script src="/Node/node_modules/art-template/lib/template-web.js"></script>
</head>
<body>
<div class ="majorBody" id="majorBody">
<div class ="header" id="header">
Comment Page:
<span class="headAside">Welcome to write your comment!</span>
</div>
<div class ="button" >
<button class="write" id="writeButton"><a href="/web/post">write</a></button>
</div>
<div class ="comment" id="commentPage">
<ul class="contentList" id="contentList">
{
{ each content }}
<li class="content" id="content">
<a href="/web/post" id="noteLink">{
{ $value.name }}说: " {
{ $value.passage }} " </a>
<p>{
{ $value.dateTime }}</p>
</li>
{
{ /each }}
</ul>
</div>
</div>
</body>
</html>
post.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post</title>
<link rel="stylesheet" href="/web/Node/css/post.css">
</head>
<body>
<form action="/form" method="GET">
<div class="MajorBody" id="MajorBody">
<div class="Header" id="Header">
<a href="/" class="majorLink">首页</a>
<span>发表言论</span>
</div>
<div class="AddressName" id="AddressName">
<input type="text" name="Name" class="NameText" id="NameText" placeholder="Please input your name!">
</div>
<div class="LeaveBoard">
<textarea type="text" name="comment" class="ContentText" id="ContentText" cols="40" rows="10" required minlength ="2" maxlength = "20" id="ContentText" placeholder="Please input your comment."></textarea>
</div>
<div class="Submit" id="Submit">
<button class="SubmitButton" id="SubmitButton" type="submit">Submit</button>
</div>
</div>
</form>
</body>
</html>