nodejs简单应用三(热部署文件)

nodejs简单应用三(热部署文件)

1、配置文件config.js

var  cg = {
  _rootPath : '/home/wuxiang/nodeapp/',
  reg_ftype : '/\.([a-z]+?)$/',
  domain    :'http://127.0.0.1:8899/',
  odomain   :true,
  ftypes    :{'gif':'image/gif',
             'html':'text/html',
 'jpg':'image/jpg',
 'png':'image/png',
 'js':'application/javascript',
 'css':'text/css',
 'htm':'text/html',
 'icon':'image/icon',
 'mp3':'audio/mpeg'}
  
}
exports.config = cg;


2、监听文件

var cp = require('child_process');
var fs = require("fs");
var http = require('http');
//var config = require('./config.js');


 function rebootProcess(exec,args){
    args = args || []
    var child = exports.child = cp.spawn(exec, args);//创建一个新线程来接力
    child.stdout.addListener("data", function (data) {
         console.log('stdoutxx: ' + data);
    });
    child.stderr.addListener("data", function (data) {
         console.log('stdouterror: ' + data);
    });
    child.addListener("exit", function () {
   console.log('new things');
        rebootProcess(exec, args);//重新做某件事

    });
}


//关闭线程
function crashProcess (prev, cur) {
    //if ( cur && +cur.mtime !== +prev.mtime|| crashProcess.status ) return;
    crashProcess.status = 1;//如果经过调整,则结束线程
    var child = exports.child;
    setTimeout(function() {
        process.kill(child.pid);
        crashProcess.status = 0;
    }, 50);
}
rebootProcess('node',['server.js']);//开始做某件事
//rebootProcess('node',['websocket.js']);//启动websocket
fs.watchFile("server.js",crashProcess);//观察文件是否改变

3、服务文件

var http    = require('http');
var fs      = require("fs");
var url     = require("url");
var path    = require("path");
var cg      = require('./config.js');




//dispatch
var dispatchRoute = function(filename,param,response){
  var t_match  = filename.match(/\.([a-z]+?[3-4]*)$/),txt_type = 'text/plain';
  if(t_match&&t_match[1])  txt_type = cg.config.ftypes[t_match[1]];
  path.exists(filename, function (exists) {
        if (!exists) {
  if(param&&param.domain){
    requestURL(param,response);
  }else{
    response.writeHead(404, {'Content-Type': txt_type});
             response.write("This request URL " + filename + " was not found on this server.");
             response.end();
  }
         
        } else {
            fs.readFile(filename, "binary", function(err, file) {
                if (err) {
                    response.writeHead(500, {'Content-Type': txt_type});
                    response.end(err);
                } else {
  
                    response.writeHead(200, {'Content-Type': txt_type});
                    response.write(file, "binary");
                    response.end();
                }
             });


          }


      });
};


var requestURL = function(options,response){
    options = {
 host: '127.0.0.1',
 port: 8080,
 path: '/web/index.html',
 method: 'GET'
}
var site = http.createClient(options.port, options.host);
var request = site.request("GET", options.path, {'host' : path.host});
    request.end();
    request.on('response', function(res) {
res.on('data', function(chunk) {
response.writeHead(res.statusCode, {'Content-Type': 'text/html'});
   response.write(chunk);
response.end();
});
            });
}
//操作数据库mysql


http.createServer(function (request, response) {
 var url_route = url.parse(request.url,true);
 var pathname  = url_route.pathname;//uri作转发
 var filename  = path.join(process.cwd(), pathname);


 var param     = url_route.query;
 //读文件
 dispatchRoute(filename,param,response);
 //操作数据库


 var mysql = require('./crudmysql.js');
 if(mysql.db){
// mysql.db.connection(response);//ss
 }
}).listen(8899);
console.log('Server running at http://127.0.0.1:8899/');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值