分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

nodejs中 图文混搭

发布时间:2023-09-06 01:30责任编辑:沈小雨关键词:jsnodejs

1、html页面代码:

<!DOCTYPE html>
<html>
<head>
<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>
</head>
<body>
<ul>
<li class="red">1</li>
<li class="blue">2</li>
<li class="green">3</li>
</ul>
<img src="./showimg"/>
</body>
</html>

2.nodejs代码:

**********************创建服务器*******************

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/")

****************************路由******************************

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);
}
}

*****************************读取文档及图片*******************************

var fs=require("fs");
module.exports={
readfile:function(path,recall){
fs.readFile(path,function(err,data){
if(err){
recall("没有找到页面!");
}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中 图文混搭

原文地址:http://www.cnblogs.com/pyj63/p/8042189.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved