javascript - 在节点 js 路由中使用外部函数
问题描述
我试图在我的路由中使用一个外部函数,它将更改应用于我的 ejs 视图。
这是我的项目的结构:
Project
functions
Evening.js
Morning.js
MyFunc.js
routes
about.js
index.js
users.js
views
about.ejs
index.ejs
app.js
另外我有我的 MyFunc.js:
var morning = require('./Morning');
var evening = require('./Evening');
module.exports = {
GetMorningMessage: function () { console.log(morning); },
GetEveningMessage: function () { console.log(evening); }
};
我知道我可以app.get()
在我的 app.js 中使用来设置所有需要的参数以将某些更改应用于我的视图,但我想知道我是否可以app.use()
在 app.js 中使用并在我的路由中调用外部函数,例如router.get()
?
例如像这样(在 index.js 中):
var morning = welcome.GetMorningMessage();
router.get('/', function (req, res) {
res.render('index', {
title: 'The index page!',
h1: 'The h1',
username: username,
currentDate: currentDate,
MyVar: morning
});
});
解决方案
推荐阅读
- python - 从python中的数据框中删除特定行
- angularjs - ng-options 抛出 object Object 作为结果
- python - aiohttp:找不到模板“index.html”
- css - 仅在没有空间将它们全部容纳在单行时才显示(包装)所有项目(没有媒体查询)
- reactjs - 自动完成问题(Material UI + React + Reagent/ClojureScript)
- python - 需要帮助使用 gpio 和 after 方法找到 tkinter 的解决方案
- javascript - 带有 axios 的 js 表单验证
- amazon-web-services - AWS-RunShellScript 中的 ssm 自动化文档输入不替换变量
- angular-spectator - 如何测试是否检查了 html 输入类型单选
- apache-spark - 从 jar 运行时,Spark 作业无法连接到 cassandra