我是靠谱客的博主 平常山水,最近开发中收集的这篇文章主要介绍node.js健壮性问题及打印错误日志,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

var moment  =  require('moment'); 
var express = require('express');
var bodyparser = require('body-parser');
var app = express();
//var router = express.Router();
var mysql = require('mysql');
var querystring = require('querystring');
var port = process.env.PORT || 8080;
var domain = require('domain');
var d = domain.create();
app.use(bodyparser.urlencoded({
    extended: true
}));
//本地请求跨域
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
var mysql = require('mysql');

//打印错误日志
var log4js = require('log4js'); //注意log4js的module位置引用是否正确
//log the cheese logger messages to a file, and the console ones as well.

//通过configure()配置log4js
log4js.configure({
    appenders: {

        cheeseLogs: {
            type: 'file',
            filename: 'cheese.log'
        },

        console: {
            type: 'console'
        }

    },
    categories: {

        cheese: {
            appenders: ['cheeseLogs'],
            level: 'error'
        },

        another: {
            appenders: ['console'],
            level: 'info'
        },

        default: {
            appenders: ['console', 'cheeseLogs'],
            level: 'trace'
        }

    }
});

const logger = log4js.getLogger('anotner');

//应用级路由
//--------------------------------------新版WIFI运营监控--------------------------------------
d.on('error', function(err) {
    console.log("68行" + err);
});
d.run(function() {
    app.post("/newwifi", function(req, res) {
        var connection = mysql.createConnection({
            host: '10.1.0.25',
            user: 'mysql',
            password: 'mysql123',
            port: '3306',
            database: 'wifi20dw'
        });
        d.on('error', function(err) {
            console.log("80行" + err);
        });
        d.run(function() {
            connection.connect();
        });
        var begintime = moment(req.body.begintime, "MM/DD/YYYY").format('YYYY-MM-DD');
        var endtime = moment(req.body.endtime, "MM/DD/YYYY").format('YYYY-MM-DD');
        let getList = "select WIFINO,DATE_FORMAT(min(mindaytime),'%Y-%m-%d %T') as `MIN TIME`,DATE_FORMAT(max(maxdaytime),'%Y-%m-%d %T') as `MAX TIME` from wifi20dw.wifidaystatic where recday between '" + begintime + "' and '" + endtime + "' group by wifino";
        connection.query(getList, function(err, rows, fields) {
            if(err) {
                logger.info("90行" + err);
            }
            logger.info(rows);
            var a = rows;

            var obj = {
                data: [],
                cols: []
            }
            var ending = false;
            a.forEach(function(v, i) {
                var arr = []
                for(var i in v) {
                    arr.push(v[i]);
                    if(ending == false) {
                        obj.cols.push({
                            title: i
                        });
                    }
                }
                ending = true;
                obj.data.push(arr);
            });

            res.send(obj);
        });
        connection.end(function(err) {
            if(err) {
                console.log("118行"+err);
            } else {

                console.log("断开连接");

            }
        });
    });
});

//var newwifi = require('../../routes/newwifi');
//app.use('/newwifi', newwifi);

//--------------------------------------新版路由器功能监控--------------------------------------
d.on('error', function(err) {
    console.log(err);
});
d.run(function() {
    app.post("/newroutermon", function(req, res) {
        var connection = mysql.createConnection({
            host: '10.1.0.25',
            user: 'mysql',
            password: 'mysql123',
            port: '3306',
            database: 'wifi20dw'
        });
        d.on('error', function(err) {
            console.log("145行" + err);
        });
        d.run(function() {
            connection.connect();
        });
        let getList = "select wifino,DATE_FORMAT(maxdaytime,'%Y-%m-%d %T')as maxdaytime,TIMESTAMPDIFF(DAY,now(),maxdaytime) as DIFFDAYS from wifi20dw.wifidaystatic group by wifino order by DIFFDAYS desc;";
        connection.query(getList, function(err, rows, fields) {
            if(err) {
                logger.info(err);
            }
            logger.info(rows);
            var a = rows;

            var obj = {
                data: [],
                cols: []
            }
            var ending = false;
            a.forEach(function(v, i) {
                var arr = []
                for(var i in v) {
                    arr.push(v[i]);
                    if(ending == false) {
                        obj.cols.push({
                            title: i
                        });
                    }
                }
                ending = true;
                obj.data.push(arr);
            });
            res.send(obj);  
        });
        connection.end(function(err) {
            if(err) {
                console.log("180行"+err);
            } else {

                console.log("断开连接");

            }
        });
    });
});
//--------------------------------------旧版WIFI运营监控--------------------------------------
d.on('error', function(err) {
    console.log(err);
});
d.run(function() {
    app.post("/oldwifi", function(req, res) {
        var connection = mysql.createConnection({
            host: '10.1.0.25',
            user: 'mysql',
            password: 'mysql123',
            port: '3306',
            database: 'wifi20dw'
        });
        d.on('error', function(err) {
            console.log("203行" + err);
        });
        d.run(function() {
            connection.connect();
        });
        var begintime = moment(req.body.begintime, "MM/DD/YYYY").format('YYYY-MM-DD');
        var endtime = moment(req.body.endtime, "MM/DD/YYYY").format('YYYY-MM-DD');

        let getList = "select WIFINO,DATE_FORMAT(min(mindaytime),'%Y-%m-%d %T') as `MIN TIME`,DATE_FORMAT(max(maxdaytime),'%Y-%m-%d %T') as `MAX TIME` from wifi20dw.wifidaystatv1 where recday between '" + begintime + "' and '" + endtime + "' group by wifino";
        connection.query(getList, function(err, rows, fields) {
            if(err) {
                logger.info(err);
            }
            logger.info(rows);
            var a = rows;

            var obj = {
                data: [],
                cols: []
            }
            var ending = false;
            a.forEach(function(v, i) {
                var arr = []
                for(var i in v) {
                    arr.push(v[i]);
                    if(ending == false) {
                        obj.cols.push({
                            title: i
                        });
                    }
                }
                ending = true;
                obj.data.push(arr);
            });

            res.send(obj);   
        });
        connection.end(function(err) {
            if(err) {
                console.log("242行"+err);
            } else {

                console.log("断开连接");

            }
        });

    });
});
//--------------------------------------旧版路由器功能监控--------------------------------------
d.on('error', function(err) {
    console.log(err);
});
d.run(function() {
    app.post("/oldroutermon", function(req, res) {
        var connection = mysql.createConnection({
            host: '10.1.0.25',
            user: 'mysql',
            password: 'mysql123',
            port: '3306',
            database: 'wifi20dw'
        });
        d.on('error', function(err) {
            console.log("145行" + err);
        });
        d.run(function() {
            connection.connect();
        });
        let getList = "select wifino,DATE_FORMAT(maxdaytime,'%Y-%m-%d %T')as maxdaytime,TIMESTAMPDIFF(DAY,now(),maxdaytime) as DIFFDAYS from wifi20dw.wifidaystatv1 group by wifino order by DIFFDAYS desc;";
        connection.query(getList, function(err, rows, fields) {
            if(err) {
                logger.info(err);
            }
            logger.info(rows);
            var a = rows;

            var obj = {
                data: [],
                cols: []
            }
            var ending = false;
            a.forEach(function(v, i) {
                var arr = []
                for(var i in v) {
                    arr.push(v[i]);
                    if(ending == false) {
                        obj.cols.push({
                            title: i
                        });
                    }
                }
                ending = true;
                obj.data.push(arr);
            });
            res.send(obj);  
        });
        connection.end(function(err) {
            if(err) {
                console.log("180行"+err);
            } else {

                console.log("断开连接");

            }
        });
    });
});
//端口监听
app.listen(8282);

最后

以上就是平常山水为你收集整理的node.js健壮性问题及打印错误日志的全部内容,希望文章能够帮你解决node.js健壮性问题及打印错误日志所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部