azat-co / proexpressjs

Examples for the Practical Node.js book [Apress, 2014]
http://proexpressjs.com
105 stars 60 forks source link

How to use domain in node.js with expressjs #6

Open mpkumar87 opened 9 years ago

mpkumar87 commented 9 years ago

Hi,

I am new to node.js and am using express framework in nodejs. I am accessing data through rest API using node-rest-client module. If API response getting delayed, then server getting disconnected and am restarting the server everytime. Also, how log the exception errors in custom log path. Below is my app.js code, Please check whether i am in right direction,

/**

var express = require('express') , util = require('util') , logger = require('morgan') , fs = require('fs') , url = require('url') , session = require('express-session') , bodyParser = require("body-parser") , cookieParser = require("cookie-parser") , favicon = require('serve-favicon') , errorHandler = require('errorhandler') , flash = require('connect-flash') , methodOverride = require('method-override');

var http = require('http'); var path = require('path'); var Client = require('node-rest-client').Client; var client = new Client();

var moment = require('moment'); var md5 = require('MD5');

global.moment = moment; global.client = client; global.md5 = md5;

// Create a write stream (in append mode) var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'}) var app = express();

// All environments app.set('port', process.env.PORT || 2000); app.set('views', dirname + '/views'); app.set('view engine', 'ejs'); app.use(favicon(dirname + '/public/images/favicon.ico')); app.use(logger("combined", {stream: accessLogStream,skip: function (req, res) { return res.statusCode < 400 }})); app.use(bodyParser.urlencoded({ extended: true, limit: '15mb' })); app.use(bodyParser.json({limit: '15mb'})); app.use(methodOverride()); app.use(cookieParser('secret')); app.use(session({secret: 'ssshhhhh',resave: true,saveUninitialized: true})); app.use(flash()); // To display flash message(success or failure) app.use(express.static(path.join(__dirname, 'public'))); // Development only if ('development' == app.get('env')) { app.use(errorHandler({ dumpExceptions: true, showStack: true })); }

app.use(function(err, req, res, next) { console.error(err.stack); res.status(500).send('Something broke!'); });

// Common middleware to execute before every request - starts here app.use(function(req, res, next) { res.locals.session_values = req.session; res.locals.success_messages = req.flash('success_messages'); res.locals.error_messages = req.flash('error_messages'); next(); }); // Common middleware to execute before every request - ends here

require('./routes/routed')(app); // File contains all request url declaration.

http.createServer(app).listen(app.get('port'), function() { console.log('Express server listening on port ' + app.get('port')); });

Also, logger is storing only http get path not errors.

Please guide me.. Waiting for all valuable reply..

Thanks, Premkumar M

mpkumar87 commented 9 years ago

Hi, Any suggestions for my above queries.. Please guide me...

Thanks, Premkumar M

azat-co commented 8 years ago

what is the error?