效果 http://www.steel-pot.com/

function handleStr(str,isHtml,callback)
{
if(!isHtml)
{
callback(str);
return;
}
str= str.replace(/<script(([\s\S])*?)<\/script>/g,"");
str= str.replace(new RegExp(dstHost,"gm"),srcHost);
callback(str);
} //html解析器
var cheerio = require('cheerio');
//文件操作模块
var fs = require('fs'),path = require('path');
//加密模块
var cryptos=require("./cryptos");
//引入http模块
var http = require("http");
//设置主机名
var hostName = '127.0.0.1';
//设置端口
var port = 9000;
var iconv = require('iconv-lite'); //创建目录
var mkdirs = module.exports.mkdirs = function(dirpath, mode, callback) {
fs.exists(dirpath, function(exists) {
if(exists) {
callback(dirpath);
} else {
//尝试创建父目录,然后再创建当前目录
mkdirs(path.dirname(dirpath), mode, function(){
fs.mkdir(dirpath, mode, callback);
});
}
});
}; //获取缓存
function getCatch(host,url,callback)
{
var file=__dirname+'/tmp/'+host.replace(':','');
mkdirs(file,777,function(){
file+='/'+cryptos.md5(url);
fs.exists(file,function(exists){
if(exists)
{
fs.readFile(file,'utf-8',function(err,data){
if(err){
callback(false,file);
}
else{
callback(true,file,data);
}
});
}else{
callback(false,file);
}
});
});
} //获取远程内容
function getRemote(hostset,url,callback)
{
var body="";
var options = {
hostname: hostset.host,
port: 80,
path:url,
method: 'GET'
};
var req = http.request(options, function (remoteRes) {
remoteRes.setEncoding(hostset.charset);
remoteRes.on('data', function (chunk) {
body+=chunk;
});
remoteRes.on("end",function(){
callback(true,body,remoteRes.headers['content-type'].indexOf("text/html") != -1);
});
});
req.on('error', function (e) {
callback(false,e.message,false);
console.log('problem with request: ' + e.message);
});
req.end();
} //获取域名
function getDstHost(host,callback)
{
fs.readFile('./hostlist.txt', 'utf8', function(err, hostlist){
hostlist=JSON.parse(hostlist); if(host in hostlist)
{
callback(hostlist[host]);
}else{
var hostset={"title":"baidu","host":"www.baidu.com"};
callback(hostset);
}
});
} //获得内容
function getContent(hostset,url,callback)
{
if(!hostset.nocatch&&url!=""&&url!="/")
{
getCatch(hostset.host,url,function(rs,file,data){
if(rs)
{
callback(data);
}else{
//获取远程内容
getRemote(hostset,url,function(rs,body,ishtml){
if(rs&&body!="")
{
handleStr(body,ishtml,function(rs){
if(file!=''){
fs.writeFile(file, rs, {flag: 'a'}, function (err) {
if(err) {
console.error(err);
}
});
}
callback(rs);
});
}else{
callback(body);
}
});
}
});
}else{
//获取远程内容
getRemote(hostset,url,function(rs,body){
handleStr(body,true,function(rs){
callback(rs);
});
});
}
} //需要保证模板文件存在,这里不判断
var tplview=function(tpl,varData,callback){
fs.readFile(tpl,'utf-8',function(err,data){
if(err)
{
callback("");
return ;
} for(key in varData)
{
re = new RegExp('{'+key+'}','g');
data = data.replace(re,varData[key]);
}
callback(data.toString());
});
}; var mainObj={};
mainObj.main=function(res,data){
var tpl=__dirname+"/index.tpl";
var varData={}; fs.readFile(__dirname+"/hostlist.txt",'utf-8',function(err,data){ data=JSON.parse(data);
varData.list="";
for(var key in data)
{
varData.list+="<div class='col-md-2'><a href='http://"+key+"'>"+data[key].title+"</a></div>";
}
tplview(tpl,varData,function(str){
res.write(str);
res.end();
});
});
};
mainObj.edit=function(res,data){
var file=__dirname+"/edit.html";
backfile(res,file);
};
mainObj.editsave=function(res,data){
var file=__dirname+"/hostlist.txt";
fs.writeFile(file, data, {}, function (err) {
if(err) {
res.write("保存文件失败");
}else{
res.write("保存成功");
}
res.end();
});
};
mainObj.getlist=function(res,data){
//设置返回值类型
res.setHeader("Content-Type", "application/json;charset=utf-8");
var file=__dirname+"/hostlist.txt";
backfile(res,file);
};
function backfile(res,file)
{
fs.exists(file,function(exists){
if(exists)
{
fs.readFile(file,'utf-8',function(err,data){
if(err){
res.statusCode=404;
res.end(err);
}
else{
res.write(data);
res.end();
}
});
}else{
res.statusCode=404;
res.end();
}
});
} var httpHandler=function(req,res){
var post="";
//接收post数据
req.on('data', function(chunk){ //通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
post += chunk;
});
req.on('end', function(){
//只支持定义好的方法,不支持未定义的方法
//处理请求
url=req.url;
//去掉第一个斜杠
url=url.substr(1);
if(url==""||url=="/")url="main"; if(!mainObj.hasOwnProperty(url))
{
res.statusCode=404;
res.end();
return;
}
mainObj[url](res,post);
});
}
//创建服务
var server = http.createServer(function(req,res){
srcHost=req.headers.host;
//如果是指定域名,则特别处理
if(srcHost=="www.steel-pot.com")
{
httpHandler(req,res);
return ;
}
//第一步获取域名
getDstHost(srcHost,function(hostset){
dstHost=hostset.host;
//第二步获取内容
getContent(hostset,req.url,function(body){
if(hostset.charset)body=iconv.encode(body, hostset.charset);
res.end(body);
});
});
});
server.listen(port,hostName,function(){
console.log('run');
});

  

nodejs 做的带管理后台的东东,主要学习到 ....我忘了学到什么了的更多相关文章

  1. Firefly卡牌手游《暗黑世界V1.5》服务器端源码+GM管理后台源码

    http://www.9miao.com/content-6-304.html Firefly卡牌手游<暗黑世界V1.5>服务器端源码+GM管理后台源码 关于<暗黑世界V1.5> ...

  2. 用php做管理后台

    最近因处理家庭之事,技术上没有提高,这段时间也陆续的恢复了正常的开发,由于要做一个管理后台,所以在选择语言和架构上搜了不少资料, php 和java 的选择上,后来选择用php作为管理后台开发的语言. ...

  3. Django基础-003 配置Django自带的后台管理,操作数据库

    插入测试数据,可以自己写页面来插入数据 也可以使用Django自带的后台管理,来操作数据表 1.创建用户 python manage.py createsuperuser 2.在浏览器输入地址,进入D ...

  4. Solr学习总结(三)Solr web 管理后台

    前面讲到了Solr的安装,按道理,这次应该讲讲.net与数据库的内容,C#如何操作Solr索引等.不过我还是想先讲一些基础的内容,比如solr查询参数如何使用,各个参数都代表什么意思? 还有solr ...

  5. Tomcat默认界面可导致版本信息泄露+管理后台爆破

    由于配置的Tomcat时,管理页面未进行删除或者权限角色配置,攻击者可以通过暴力猜解进入到管理后台,从而上传获取shell. Tomcat的默认工具manager配置,在很多的生产环境中由于基本用不到 ...

  6. Admin管理后台

    Django奉行Python的内置电池哲学.它自带了一系列在Web开发中用于解决常见问题或需求的额外的.可选工具.这些工具和插件,例如django.contrib.redirects都必须在setti ...

  7. django学习-22.admi管理后台页面的文案展示等相关配置

    目录结果 1.前言 2.完整的操作步骤 2.1.第一步:对[settings.py]里的相关常量的值做如下修改 2.2.第二步:重启django项目[helloworld]的服务 2.3.第三步:重新 ...

  8. 管理后台Vue

    管理后台 遇到的问题 搭建 基于vue 3.0 Vue CLI 4.x Ant Design Vue 2.0 搭建后台管理系统 Ant Design Vue 2.0 npm i --save ant- ...

  9. 从0到1用react+antd+redux搭建一个开箱即用的企业级管理后台系列(基础篇)

    背景 ​ 最近因为要做一个新的管理后台项目,新公司大部分是用vue写的,技术栈这块也是想切到react上面来,所以,这次从0到1重新搭建一个react项目架子,需要考虑的东西的很多,包括目录结构.代码 ...

随机推荐

  1. springboot集成邮件服务

    一.前言 Spring Email 抽象的核心是 MailSender 接口,MailSender 的实现能够把 Email 发送给邮件服务器,由邮件服务器实现邮件发送的功能. Spring 自带了一 ...

  2. 《Java并发编程实战》读书笔记(一)----- 简介

    简史 早期的计算机中不包含操作系统,从头至尾都只执行一个程序,并且这个程序能访问计算机所有资源.随着计算机发展,操作系统的出现,使得计算机可以同时运行多个程序,并且每程序都在单独的进程内运行.为什么要 ...

  3. maven更改仓库地址

    安装maven后,maven的默认的仓库地址在  C:\Users\Administrator\.m2\repository 修改maven的仓库地址的步骤是,1.在某个盘符下建立一个文件夹,当做现在 ...

  4. Spring 框架(三)

    1 spring l AOP :切面编程 切面:切入点 和 通知 结合 l spring aop 编程 <aop:config> 方法1: <aop:pointcut express ...

  5. 《Visual C++ 2010入门教程》系列五:合理组织项目、使用外部工具让工作更有效

    原文:http://www.cnblogs.com/Mrt-02/archive/2011/07/24/2115631.html 这一章跟大家分享一些与c++项目管理.VAX.SVN.VS快捷键等方面 ...

  6. 【NLP_Stanford课堂】语言模型3

    一.产生句子 方法:Shannon Visualization Method 过程:根据概率,每次随机选择一个bigram,从而来产生一个句子 比如: 从句子开始标志的bigram开始,我们先有一个( ...

  7. ubuntu 启用root用户方法

    1.按下ctrl + alt + T,输入 sudo passwd root设置root的密码,如下图所示: 2.使用su root来测试是否可以进入root用户,如果出现#说明已经设置root用户的 ...

  8. Python初学者第五天 列表及简单操作

    5day 数据类型:列表 1.创建列表 user = ['aa','14',1,10,'aa',1,2,3,3,5,9] n = [] list() m = list() 2.查询 a.按索引查询 b ...

  9. typeof操作符和instanceof操作符的区别 标签: JavaScript 2016-08-01 14:21 113人阅读 评论(

    typeof主要用于检测变量是不是基本数据类型 typeof操作符是确定一个变量是字符串.数值.布尔类型,还是undefined的最佳工具.此外,使用typeof操作符检测函数时,会返回"f ...

  10. SAP人工智能服务Recast.AI的一个简单例子

    关于这个例子的完整介绍,请参考公众号 "汪子熙"的两篇文章: SAP C/4HANA与人工智能和增强现实(AR)技术结合的又一个创新案例 和使用Recast.AI创建具有人工智能的 ...