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&¶m.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/');

你可能感兴趣的:(NodeJS)