来自控制器的 var 通过 app.js 查看
var from controller to view via app.js
我实际上正在学习 node.js 使用 expressjs,我有一些具有结构的项目:
controllers/Home.js
和
views/index.ejs
在我的根目录中,我有 app.js 代码:
app.all('/', function(req, res, next) {
Home.index();
res.render('index', {
title: 'Home'
});
});
...但我想从我的控制器文件 (Home.js) 和函数 index 发送标题,你能解释一下如何做到这一点吗?
此致。
选项 1:
公开 home.js 文件中的标题并在 app.js 中访问它:
var home = require('home_path');
app.all('/', function(req, res, next) {
res.render('index', {
title: home.title
});
});
选项 2:
创建额外的 some_file.js 获取 title
并传递给 some_handler_function
作为:
var some_file = require('path_to_some_file');
app.all('/', some_handler_function);
Node-Cheat可用:
要获得完整的代码,请在 node-cheat ejs_variable_access 运行 node server
开始工作,然后是 npm install
。
我实际上正在学习 node.js 使用 expressjs,我有一些具有结构的项目:
controllers/Home.js 和 views/index.ejs
在我的根目录中,我有 app.js 代码:
app.all('/', function(req, res, next) {
Home.index();
res.render('index', {
title: 'Home'
});
});
...但我想从我的控制器文件 (Home.js) 和函数 index 发送标题,你能解释一下如何做到这一点吗?
此致。
选项 1:
公开 home.js 文件中的标题并在 app.js 中访问它:
var home = require('home_path');
app.all('/', function(req, res, next) {
res.render('index', {
title: home.title
});
});
选项 2:
创建额外的 some_file.js 获取 title
并传递给 some_handler_function
作为:
var some_file = require('path_to_some_file');
app.all('/', some_handler_function);
Node-Cheat可用:
要获得完整的代码,请在 node-cheat ejs_variable_access 运行 node server
开始工作,然后是 npm install
。