1. HTML page code:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title></title>
<style type= "Text/css" >
*{
margin:0;
padding:0;
}
UL li{
Float:left;
width:200px;
height:50px;
Text-align:center;
List-style:none;
border:0.1px solid #000;
line-height:50px;
Color: #fff;
}
. red{
background:red;
}
. blue{
Background:blue;
}
. green{
Background:green;
}
</style>
<body>
<ul>
<li class= "Red" >1</li>
<li class= "Blue" >2</li>
<li class= "Green" >3</li>
</ul>
</body>
2.nodejs Code:
Create Server *******************
var http=require ("http");
var url=require ("url");
var router=require ("./02.js")
Http.createserver (function (req,res) {
if (req.url!= "/favicon.ico") {
Pathname=url.parse (Req.url). Pathname;
Pathname=pathname.replace (/\//, "");
Console.log (pathname);
try{
Router[pathname] (req,res);
}catch (e) {
Console.log ("11")
Console.log (e)
}
}
}). Listen (8000);
Console.log ("Server running at Http://127.0.0.1:8000/")
Routing ******************************
var optfile=require ("./03.js")
module.exports={
Login:function (req,res) {
Res.writehead (200,{"Content-type": "Text/html", CharSet: ' Utf-8 '});
function recall (data) {
Res.write (data);
Res.end ("");
}
Optfile.readfile ("index.html", recall);
},
Showimg:function (req,res) {
Res.writehead (200,{"Content-type": "Image/jpeg"});
Optfile.readimg ("./img/w.jpg", res);
}
}
Reading documents and Pictures *******************************
var fs=require ("FS");
module.exports={
Readfile:function (Path,recall) {
Fs.readfile (Path,function (err,data) {
if (err) {
Recall ("No page found!") ");
}else{
Console.log (Data.tostring ());
Recall (data);
}
})
},
Readimg:function (path,res) {
Fs.readfile (Path, "binary", function (err,file) {
if (err) {
Console.log (ERR);
}else{
Res.write (file, "binary");
Res.end ("");
}
})
}
}
Nodejs in the text mix