node.js - NodeJs website TTFB is very high and site is running very slow -
i have created website in nodejs express & jade. when load simple login page less content, taking time load. it's ttfb high (ref screenshot attached.) below app.js code. not sure i'm doing wrong.
app.js
// packages var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var dbhelper = require('./routes/dbhelper.js') var common = require('./routes/common.js') var session = require('express-session') var http = require('http'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(session({ secret: 'keyboard cat', resave: false, saveuninitialized: true, cookie: { secure: true } })) // uncomment after placing favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: false })); app.use(cookieparser()); app.use(require('stylus').middleware(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public'))); //app.locals.basedir = path.join(__dirname, 'views'); // set mustbe config var mustbe = require("mustbe"); var mustbeconfig = require("./mustbeconfig"); mustbe.configure(mustbeconfig); // routes var routes = require('./routes/index'); var users = require('./routes/users'); var dashboard = require('./routes/dashboard'); var monitor = require('./routes/monitor'); var resetpassword = require('./routes/resetpassword'); var archive = require('./routes/archive'); var archivelist = require('./routes/archivelist'); var archivesetup = require('./routes/archivesetup'); app.locals.moment = require('moment'); app.use(function (req, res, next) { res.locals.session = session; next(); }); // make database connection available router app.use(function (req, res, next) { req.sessionid = req.sessionid; req.db = dbhelper.sql; req.config = dbhelper.config; req.actionoutput = common.actionoutput; req.actionstatus = common.actionstatus; req.changeemployer = common.changeemployer; // redirect login page if no session found if ((session.employee == null || session.employee == undefined || session.employee == '') && (req.url != '/' && req.url != '' && req.url != '/login')) res.redirect('/'); // redirect dashboard if session exists & redirecting login page else if ((session.employee != null && session.employee != undefined && session.employee != '') && (req.url == '/' || req.url == '' || req.url == '/login')) res.redirect('/dashboard'); else next(); }); app.use('/', routes); app.use('/users', users); app.use('/dashboard', dashboard); app.use('/monitor', monitor); app.use('/resetpassword', resetpassword); app.use('/archive', archive); app.use('/archivelist', archivelist) app.use('/archivesetup', archivesetup); process.on('uncaughtexception', function (err) { console.log('caught exception: ${ err}'); var eid = saveerrorlog(err.message, err.stack, 'a', err.address); }); app.use(function (err, req, res, next) { if (!err) return next(); var eid = saveerrorlog(err.message, err.stack, req.connection.remoteaddress , err.path); // check if request ajax var is_ajax_request = req.xhr; var op = new common.actionoutput(); op.actionstatus = common.actionstatus.error; op.message = err.message; res.send(op); }); function saveerrorlog(message, completeerror, ip, url) { // logged in user var userid = null; if (session.supportuser != null && session.supportuser != undefined && session.supportuser != '') { userid = session.supportuser.userid; } else if (session.employee != null && session.employee != undefined && session.employee != '') { userid = session.employee.employeeid; } // log error database var errorlogid = 0; var sql = dbhelper.sql; var conn = new sql.connection(dbhelper.config); conn.connect().then(function () { // stored procedure var request = new sql.request(conn); request.input('userid', sql.bigint, userid); request.input('message', sql.varchar(500), message); request.input('completeerror', sql.varchar(2000), completeerror); request.input('ip', sql.varchar(50), ip); request.input('url', sql.varchar(50), url); request.execute('usp_saveerrorlog', function (err, recordset, returnvalue) { if (!err && recordset[0][0].validationcode == null) { errorlogid = recordset[0][0].errorlogid; } else { } return errorlogid; }); }); } // catch 404 , forward error handler app.use(function (req, res, next) { var err = new error('not found'); err.status = 404; next(err); }); // error handlers // development error handler // print stacktrace if (app.get('env') === 'development') { app.use(function (err, req, res, next) { res.status(err.status || 500); res.render('shared/error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked user app.use(function (err, req, res, next) { res.status(err.status || 500); res.render('shared/error', { message: err.message, error: {} }); }); module.exports = app; //app.listen(1234, function () { // console.log('server starts on port: ' + 1234); //}); http.createserver(app).listen(1234, function () { console.log('express server listening on port ' + 1234); });
i have removed database related work app.js still facing same issue. appreciable.
we have used this, cache jade content , reduce ttfb
/* home page. */ router.get('/', function (req, res) { var abs = session.employers; var options = {cache: true , title: 'express'}; //method 1 //var template = jade.compilefile('./views/login/index.jade', options); //res.send(template({ title: 'express' })); //method 2 res.send(jade.renderfile('./views/login/index.jade', options)); });
Comments
Post a Comment