Nodejs Learning: Express Local, response global variable __js

Source: Internet
Author: User

Turn from: http://qianduanblog.com/post/nodejs-learning-9-express-local-response-variable.html

In doing applications, web programs, and so on, you need to configure the global variables beforehand, rather than writing the program as hard coded. Local global variables, which are variables that are reused in multiple places, general class refers to static constants, such as Web site name, address, description, author and other information, and response to global variables, refers to the repeated use of dynamic variables, general class refers to access to the user ID, mailbox, points, user name and other information.

In PHP, you can use $_globals, define, and so on to declare global variables, use the Window object in JS to declare global variables, and in Nodejs the global object. 1, global variables

So, is it possible to use global declaration variables in a project? There is such a app5. /app5/app.js/app5/routes/index.js ... Other omitted

Add a global.test variable to the/app5/app.js:

/** * Module dependencies. * * global.test = "ydr.me"; var express = require (' Express '); var routes = require ('./routes '); var user = require ('./routes/user '); var http = require (' http '); var path = require (' path '); var app = Express (); // ... The following omitted

Modified in/app5/routes/index.js to:

/* Get home page. * * Console.log (global.test); Exports.index = function (req, res) {Console.log (global.test); Res.render (' index ', {title: ' Express '});

Start the app and then visit Http://localhost:3000/, which you can see in the cmd window:

Because the Nodejs service is single-threaded, the global variable is local globally and cannot complete the purpose of the response variable, meaning that the local variable is not relevant to the request, but is related only to the current process. 2, app, response variable transmission

If you do not want to use global to declare a globally variable, you can also use object-passing methods to simulate the purpose of obtaining global variables, especially in Nodejs, a modular program that is more flexible and ingenious.

/app5/app.js modified to:

/** * Module dependencies. * * var express = require (' Express '); var routes = require ('./routes '); var user = require ('./routes/user '); var http = require (' http '); var path = require (' path '); var app = Express (); App.locals.test = "Ydr.me"; Routes=routes (APP); // ... The following omitted

/app5/routes/index.js modified to:

/* Get home page. *//Portal Delivery App Object module.exports = function (APP) {console.log (app.locals.test); return {index:function (req, res) {cons Ole.log (app.locals.test); Res.render (' index ', {title: ' Express '}); }, }; }

This, of course, is consistent with the result of the first use of global storage for local global variables. In Express, requests (request), Response (response), and next (next) three variables are passed globally and can be used to store global response variables.

/app5/app.js modified to:

/** * Module dependencies. * * var express = require (' Express '); var routes = require ('./routes '); var user = require ('./routes/user '); var http = require (' http '); var path = require (' path '); var app = Express (); Local global variable app.locals.test = "Ydr.me"; Routes=routes (APP); App.use (function (request, response, Next) {//Response global variable response.locals.time=date.now (); next ();}); // ... The following omitted

/app5/routes/index.js modified to:

/* Get home page. *//Portal Delivery App Object module.exports = function (APP) {console.log (app.locals.test); </

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.