CNode

express,app怎么传到controller上?

问答
Ccoolicer发布于12 年前最后回复12 年前16 回复5421 浏览0 收藏

我用了webstorm新建一个项目,然后:

app.js

app.locals.title = '123'; (不要这个) app.set('xxxx','xxxxxxxxxxxx')

routers/index.js

var express = require('express'); var router = express.Router(); var app = require('../app'); console.log(1,app.get('xxxx)); ---> 没有get方法 /* GET home page. */ router.get('/', function(req, res) { res.render('index', { title: 'Express' }); });

module.exports = router;

查看回复

回复 (16)

M
MiguelValentine#1·12 年前

。。。 app.js最后加上 module.export = app;

C
coolicer#2·12 年前
引用 MiguelValentine。。。 app.js最后加上 module.export = app;

@MiguelValentine 新建就有这一条的。只是我没写出来,而且你还写错了,少了个s

M
MiguelValentine#3·12 年前

其实我推荐的的写法是 conf.js var conf = {} conf.title = 'xxx'

router/index require('../conf.js') console.log(conf.title)

//其实说到底,直接用require,不需要exports。

M
MiguelValentine#4·12 年前
引用 coolicer@MiguelValentine 新建就有这一条的。只是我没写出来,而且你还写错了,少了个s

@coolicer app.locals输出{} 因为他是应该个OBJECT类,还是空的 但是app.locals.title是可以打印出来的

C
coolicer#5·12 年前
引用 MiguelValentine其实我推荐的的写法是 conf.js var conf = {} conf.title = 'xxx' router/index require('../conf.js') console.l...

@MiguelValentine 有点离题了,我主要是想知道App能不能到 routers 上

M
MiguelValentine#6·12 年前
引用 coolicer@MiguelValentine 有点离题了,我主要是想知道App能不能到 routers 上

@coolicer 已经传过去了,app.locals没传过去的话,会报错的

M
MiguelValentine#8·12 年前
引用 coolicer@MiguelValentine 。

打印的是什么? app.locals.title

C
coolicer#9·12 年前
引用 MiguelValentine打印的是什么? app.locals.title

@MiguelValentine 其实我是想把app弄过去

M
MiguelValentine#10·12 年前
引用 coolicer@MiguelValentine 其实我是想把app弄过去

@coolicer 我懂了,等一下,我查个API

M
MiguelValentine#11·12 年前
引用 coolicer@MiguelValentine 其实我是想把app弄过去

@coolicer var app = express(); 不知道用在中间件里有没有冲突,我也没试过

M
MiguelValentine#13·12 年前
引用 coolicer@MiguelValentine 我好像试过,但是它是另一个App了。纠结啊

@coolicer app.js app.use(router(app))

router var app; module.exports = paser; var paser = function(appx){ app = appx; router; }

按照这个思路改改

J
jysperm#14·12 年前

用 Router 啊

C
coolicer#15·12 年前
引用 jysperm用 Router 啊

@jysperm 怎么用

参与回复
登录后即可参与回复。登录