Hi I have some newbie questions about the use of res (Express response object) and res.locals in Express.
While studying nodejs in one of the code examples There is a middleware (messages.js), a server (app.js) and the template (messages.ejs). Looking into the sample code for the template. It appears that although messages and removeMessages() is assigned to res.locals. You can access them using messages or removeMessages() without prefixing the call with locals. I wish to know the following:
Sample Code
messages.js
var express = require('express'); var res = express.response; res.message = function (msg, type) { type = type || 'info' var sess = this.req.session; sess.messages = sess.messages || []; sess.messages.push({ type: type, string: msg }); }; res.error = function (msg) { return this.message(msg, 'error'); }; module.exports = function (req, res, next) { res.locals.messages = req.session.messages || []; res.locals.removeMessages = function () { req.session.messages = []; }; next(); };
app.js(partial code)
var express = require('express'); var messages = require('./lib/messages'); var app = express(); app.use(messages);
messages.ejs
<% if(locals.messages) { %> <% messages.forEach(function (message) { % %> <p class = '<%= message.type %>' > <%= message.string %> < /p> <% }) %> <% removeMessages(); %> <% } %>
The res. locals is an object that contains the local variables for the response which are scoped to the request only and therefore just available for the views rendered during that request or response cycle.
The app. locals object defines the properties that are local variables inside an application. Once the value of app. locals property is set, it persists throughout the life of the application.
The res. send function sets the content type to text/Html which means that the client will now treat it as text. It then returns the response to the client.
HTTP is an independent module. Express is made on top of the HTTP module. HTTP module provides various tools (functions) to do things for networking like making a server, client, etc. Express along with what HTTP does provide many more functions in order to make development easy.
res.locals
is an object passed to whatever rendering engine your app is using (in this case ejs
). They'll be 'global' in the render, so you don't need to prepend anything on to them to use them.
Say we wanted our server to pick up our JavaScript from S3 when in production mode, but use the local copies when in development. res.locals
makes this easy. We'd have middleware along these lines in app.js:
if ('production' === app.get('env')) { res.locals.jsLocation = 'https://s3.amazonaws.com/kittens/js/' } else { res.locals.jsLocation = '/js/'; }
and index.ejs
would be something like this:
<script src="<%= jsLocation %>angular.min.js"></script> <script src="<%= jsLocation %>myAngularFile.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With