express logging response body

Not sure if it’s the simplest solution, but you can write a middleware to intercept data written to the response. Make sure you disable app.compress(). function logResponseBody(req, res, next) { var oldWrite = res.write, oldEnd = res.end; var chunks = []; res.write = function (chunk) { chunks.push(chunk); return oldWrite.apply(res, arguments); }; res.end = function (chunk) … Read more

What’s the difference between “app.render” and “res.render” in express.js?

Here are some differences: You can call app.render on root level and res.render only inside a route/middleware. app.render always returns the html in the callback function, whereas res.render does so only when you’ve specified the callback function as your third parameter. If you call res.render without the third parameter/callback function the rendered html is sent … Read more

How to separate routes on Node.js and Express 4?

Server.js var express = require(‘express’); var app = express(); app.use(express.static(‘public’)); //Routes app.use(require(‘./routes’)); //http://127.0.0.1:8000/ http://127.0.0.1:8000/about //app.use(“/user”,require(‘./routes’)); //http://127.0.0.1:8000/user http://127.0.0.1:8000/user/about var server = app.listen(8000, function () { var host = server.address().address var port = server.address().port console.log(“Example app listening at http://%s:%s”, host, port) }) routes.js var express = require(‘express’); var router = express.Router(); //Middle ware that is specific to … Read more

What’s the best practice for expressjs logging?

We use winston, it’s probably the most robust logging package out there. We ended up setting it up exactly like you suggested. Creating a common library used for wrapping the logger object around our definitions and transports, and then handling any other type of objects we want to be handled differently. https://gist.github.com/rtgibbons/7354879

Verify if my node.js instance is dev or production

Normally you should run a node app in production like this: NODE_ENV=production node app.js Applications with Express, Socket.IO and other use process.env.NODE_ENV to figure out the environment. In development you can omit that and just run the app normally with node app.js. You can detect the environment in your code like this: var env = … Read more