Node.js 在两条快捷路线之间共享一个变量

Node.js 在两条快捷路线之间共享一个变量,node.js,express,Node.js,Express,我想知道是否有一种方法可以在expressJS中的两条路由之间共享变量,而无需将其声明为全局变量。我有以下的路线: exports.routeOne = (req,res) => { var myVariable='this is the variable to be shared'; } exports.routeTwo = (req,res) => { //need to access myVariable here } 如何做到这一点?===============

我想知道是否有一种方法可以在expressJS中的两条路由之间共享变量,而无需将其声明为全局变量。我有以下的路线:

exports.routeOne = (req,res) => { 
 var myVariable='this is the variable to be shared';
}

exports.routeTwo = (req,res) => {
  //need to access myVariable here
}
如何做到这一点?

===============

注意

这个答案回答了一个错误的问题,但是对于那些像我一样误解了这个问题的人来说,它可能仍然有用,所以我现在把它保留在这里

===============

为了使变量存在,它必须首先执行
routeOne
,然后执行
routewo
。这是express中非常常见的场景。要获得关于如何工作的详细信息,请阅读中间件()并了解每个路由都是中间件

这里常见的模式解决方案是向存储变量的
req
res
对象添加一个新属性。然后告诉路由调用下一个中间件。下一个中间件可以访问相同的
req
res
,因此它还可以访问刚才存储的属性和值

与大多数中间件一样,这种做法没有什么错。例如,body解析器中间件()

下面是代码运行方式的示例:

routes.js

exports.routeOne = (req,res,next) => { 
 req.myVariable='this is the variable to be shared'
 next()
}

exports.routeTwo = (req,res) => {
  //need to access myVariable here
  console.log(req.myVariable)
}
const express = require('express')
const routes = require('./routes.js)

const app = express()
app.use(routes.routeOne)
app.use(routes.routeTwo)

app.listen(3000)
let variableScopedToModule = 0

exports.routeOne = (req, res) {
  variableScopedToModule++
}

exports.routeTwo = (req, res) {
  console.log(variableScopedToModule)
}
const db = require('./my-db-controller')

let cachedValue

exports.routeOne = async (req, res) {
  if (cachedValue === undefined) cachedValue = await db.getValue()
  cachedValue++
  db.setValue(cachedValue)
}

exports.routeTwo = (req, res) {
  console.log(cachedValue)
}
index.js

exports.routeOne = (req,res,next) => { 
 req.myVariable='this is the variable to be shared'
 next()
}

exports.routeTwo = (req,res) => {
  //need to access myVariable here
  console.log(req.myVariable)
}
const express = require('express')
const routes = require('./routes.js)

const app = express()
app.use(routes.routeOne)
app.use(routes.routeTwo)

app.listen(3000)
let variableScopedToModule = 0

exports.routeOne = (req, res) {
  variableScopedToModule++
}

exports.routeTwo = (req, res) {
  console.log(variableScopedToModule)
}
const db = require('./my-db-controller')

let cachedValue

exports.routeOne = async (req, res) {
  if (cachedValue === undefined) cachedValue = await db.getValue()
  cachedValue++
  db.setValue(cachedValue)
}

exports.routeTwo = (req, res) {
  console.log(cachedValue)
}
===============

注意

这个答案回答了一个错误的问题,但是对于那些像我一样误解了这个问题的人来说,它可能仍然有用,所以我现在把它保留在这里

===============

为了使变量存在,它必须首先执行
routeOne
,然后执行
routewo
。这是express中非常常见的场景。要获得关于如何工作的详细信息,请阅读中间件()并了解每个路由都是中间件

这里常见的模式解决方案是向存储变量的
req
res
对象添加一个新属性。然后告诉路由调用下一个中间件。下一个中间件可以访问相同的
req
res
,因此它还可以访问刚才存储的属性和值

与大多数中间件一样,这种做法没有什么错。例如,body解析器中间件()

下面是代码运行方式的示例:

routes.js

exports.routeOne = (req,res,next) => { 
 req.myVariable='this is the variable to be shared'
 next()
}

exports.routeTwo = (req,res) => {
  //need to access myVariable here
  console.log(req.myVariable)
}
const express = require('express')
const routes = require('./routes.js)

const app = express()
app.use(routes.routeOne)
app.use(routes.routeTwo)

app.listen(3000)
let variableScopedToModule = 0

exports.routeOne = (req, res) {
  variableScopedToModule++
}

exports.routeTwo = (req, res) {
  console.log(variableScopedToModule)
}
const db = require('./my-db-controller')

let cachedValue

exports.routeOne = async (req, res) {
  if (cachedValue === undefined) cachedValue = await db.getValue()
  cachedValue++
  db.setValue(cachedValue)
}

exports.routeTwo = (req, res) {
  console.log(cachedValue)
}
index.js

exports.routeOne = (req,res,next) => { 
 req.myVariable='this is the variable to be shared'
 next()
}

exports.routeTwo = (req,res) => {
  //need to access myVariable here
  console.log(req.myVariable)
}
const express = require('express')
const routes = require('./routes.js)

const app = express()
app.use(routes.routeOne)
app.use(routes.routeTwo)

app.listen(3000)
let variableScopedToModule = 0

exports.routeOne = (req, res) {
  variableScopedToModule++
}

exports.routeTwo = (req, res) {
  console.log(variableScopedToModule)
}
const db = require('./my-db-controller')

let cachedValue

exports.routeOne = async (req, res) {
  if (cachedValue === undefined) cachedValue = await db.getValue()
  cachedValue++
  db.setValue(cachedValue)
}

exports.routeTwo = (req, res) {
  console.log(cachedValue)
}

NodeJS最棒的一点是,您可以将状态存储在变量中。运行NodeJS服务器的进程将继续运行,而不会为传入的每个请求重新启动。因此,使用
GLOBAL.myVarName
存储的任何变量或存储在模块范围内的变量(单个JavaScript文件)都将保持不变。。。直到过程结束。如果服务器宕机或抛出未捕获的错误或其他异常,进程可能会退出。您可以设置一个流程管理器来保持其运行,但是内存中的变量现在丢失了

我建议使用范围更高的变量进行缓存,但如果数据很重要,则应根据需要将其存储到数据库并从数据库中读取

下面是您的routes.js模块的一个示例。您在函数之外声明的任何变量的作用域都是此文件。换句话说,这些变量是该文件的全局变量。你可以在和上阅读更多关于这方面的内容

routes.js

exports.routeOne = (req,res,next) => { 
 req.myVariable='this is the variable to be shared'
 next()
}

exports.routeTwo = (req,res) => {
  //need to access myVariable here
  console.log(req.myVariable)
}
const express = require('express')
const routes = require('./routes.js)

const app = express()
app.use(routes.routeOne)
app.use(routes.routeTwo)

app.listen(3000)
let variableScopedToModule = 0

exports.routeOne = (req, res) {
  variableScopedToModule++
}

exports.routeTwo = (req, res) {
  console.log(variableScopedToModule)
}
const db = require('./my-db-controller')

let cachedValue

exports.routeOne = async (req, res) {
  if (cachedValue === undefined) cachedValue = await db.getValue()
  cachedValue++
  db.setValue(cachedValue)
}

exports.routeTwo = (req, res) {
  console.log(cachedValue)
}
如果永远不要松开变量状态这一点很重要,并且假设您没有在多个进程上运行服务器以实现负载平衡,那么您可以执行以下操作:

routes.js

exports.routeOne = (req,res,next) => { 
 req.myVariable='this is the variable to be shared'
 next()
}

exports.routeTwo = (req,res) => {
  //need to access myVariable here
  console.log(req.myVariable)
}
const express = require('express')
const routes = require('./routes.js)

const app = express()
app.use(routes.routeOne)
app.use(routes.routeTwo)

app.listen(3000)
let variableScopedToModule = 0

exports.routeOne = (req, res) {
  variableScopedToModule++
}

exports.routeTwo = (req, res) {
  console.log(variableScopedToModule)
}
const db = require('./my-db-controller')

let cachedValue

exports.routeOne = async (req, res) {
  if (cachedValue === undefined) cachedValue = await db.getValue()
  cachedValue++
  db.setValue(cachedValue)
}

exports.routeTwo = (req, res) {
  console.log(cachedValue)
}

第二个解决方案更复杂,因为您需要理解和。此外,如果您使用load balencer,除非您使用Redis之类的工具,否则此解决方案将不起作用。或者,您可以针对每个请求对数据库进行读写,而不是使用内存缓存。

NodeJS最棒的一点是,您可以将状态存储在变量中。运行NodeJS服务器的进程将继续运行,而不会为传入的每个请求重新启动。因此,使用
GLOBAL.myVarName
存储的任何变量或存储在模块范围内的变量(单个JavaScript文件)都将保持不变。。。直到过程结束。如果服务器宕机或抛出未捕获的错误或其他异常,进程可能会退出。您可以设置一个流程管理器来保持其运行,但是内存中的变量现在丢失了

我建议使用范围更高的变量进行缓存,但如果数据很重要,则应根据需要将其存储到数据库并从数据库中读取

下面是您的routes.js模块的一个示例。您在函数之外声明的任何变量的作用域都是此文件。换句话说,这些变量是该文件的全局变量。你可以在和上阅读更多关于这方面的内容

routes.js

exports.routeOne = (req,res,next) => { 
 req.myVariable='this is the variable to be shared'
 next()
}

exports.routeTwo = (req,res) => {
  //need to access myVariable here
  console.log(req.myVariable)
}
const express = require('express')
const routes = require('./routes.js)

const app = express()
app.use(routes.routeOne)
app.use(routes.routeTwo)

app.listen(3000)
let variableScopedToModule = 0

exports.routeOne = (req, res) {
  variableScopedToModule++
}

exports.routeTwo = (req, res) {
  console.log(variableScopedToModule)
}
const db = require('./my-db-controller')

let cachedValue

exports.routeOne = async (req, res) {
  if (cachedValue === undefined) cachedValue = await db.getValue()
  cachedValue++
  db.setValue(cachedValue)
}

exports.routeTwo = (req, res) {
  console.log(cachedValue)
}
如果永远不要松开变量状态这一点很重要,并且假设您没有在多个进程上运行服务器以实现负载平衡,那么您可以执行以下操作:

routes.js

exports.routeOne = (req,res,next) => { 
 req.myVariable='this is the variable to be shared'
 next()
}

exports.routeTwo = (req,res) => {
  //need to access myVariable here
  console.log(req.myVariable)
}
const express = require('express')
const routes = require('./routes.js)

const app = express()
app.use(routes.routeOne)
app.use(routes.routeTwo)

app.listen(3000)
let variableScopedToModule = 0

exports.routeOne = (req, res) {
  variableScopedToModule++
}

exports.routeTwo = (req, res) {
  console.log(variableScopedToModule)
}
const db = require('./my-db-controller')

let cachedValue

exports.routeOne = async (req, res) {
  if (cachedValue === undefined) cachedValue = await db.getValue()
  cachedValue++
  db.setValue(cachedValue)
}

exports.routeTwo = (req, res) {
  console.log(cachedValue)
}

第二个解决方案更复杂,因为您需要理解和。此外,如果您使用load balencer,除非您使用Redis之类的工具,否则此解决方案将不起作用。或者,您可以针对每个请求对数据库进行读写操作,而不是使用内存缓存。

对于要从一个请求保存到下一个请求的用户特定数据,您不能只将数据存储在简单的服务器端变量中,因为服务器端变量可以在向您的服务器发出请求的所有用户之间自由共享

存储特定于用户的服务器端数据的常用方法是使用会话对象。简而言之,当