Node.js:配置和路由在不同的文件中
Node.js: Configuration and routes in a different file
我正在启动一个新的Node.js应用程序,这一次,我试图正确地组织代码,而不是把所有东西都放在同一个文件中。
我现在只有一个简单的设置在server.coffee
:
express = require 'express'
app = module.exports = express.createServer()
## CONFIGURATION ##
app.configure () ->
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.use express.bodyParser()
app.use express.logger('dev')
app.use express.profiler()
app.use express.methodOverride()
app.use app.router
app.use express.static(__dirname + '/public')
app.configure 'development', () ->
app.use express.errorHandler({dumpExceptions: true, showStack: true})
app.configure 'production', () ->
app.use express.errorHandler()
app.get '/', (req,res) ->
res.render 'index'
title: 'Express'
## SERVER ##
port = process.env.PORT || 3000
app.listen port, () ->
console.log "Listening on port" + port
关于这段简单的代码,我有一些问题,我知道所有的答案都取决于开发人员,但我真的想把它做好:
-
server.js
文件应该多于app.listen
文件吗?那里到底应该有什么? - 所有的配置不应该和路由放在一个不同的文件里吗?当我运行
server.coffee
时,我如何将app.get
删除到其他文件并使它们工作? - 我在Hubot等许多应用程序中看到的
index.coffee
究竟应该包含什么?
我希望有人能给我一个答案,而不是"视情况而定"。
您可以利用require
,并简单地将app
变量作为参数传递给方法。这不是最漂亮的语法,也不是CoffeeScript中的语法,但您应该了解其中的含义。
routes.js
module.exports = function (app) {
// set up the routes themselves
app.get("/", function (req, res) {
// do stuff
});
};
app.js
require("./routes")(app);
如果你想更进一步,我把我的路由分成更小的组,并在它自己的子文件夹中。(如:routes/auth.js
用于登录/注销,routes/main.js
用于home/about/contact等)
路线/index.js
// export a function that accepts `app` as a param
module.exports = function (app) {
require("./main")(app);
// add new lines for each other module, or use an array with a forEach
};
(将之前的routes.js
重命名为routes/main.js
,源本身保持不变)
Express 4用express.Router
类简化了这一点。
另一个帮助组织路由的特性是一个新的类
express.Router
,您可以使用它来创建模块化的可安装路由处理程序。Router
实例是一个完整的中间件和路由系统;因此,它通常被称为"迷你应用程序"。下面的例子将路由器创建为一个模块,在其中加载一个中间件,定义一些路由,并将其挂载到主应用的某个路径上。
在app目录下创建一个名为
birds.js
的路由器文件,内容如下:var express = require('express'); var router = express.Router(); // middleware specific to this router router.use(function timeLog(req, res, next) { console.log('Time: ', Date.now()); next(); }); // define the home page route router.get('/', function(req, res) { res.send('Birds home page'); }); // define the about route router.get('/about', function(req, res) { res.send('About birds'); }); module.exports = router;
然后,在应用中加载router模块:
var birds = require('./birds'); app.use('/birds', birds);
应用程序现在将能够处理到
/birds
和/birds/about
的请求,以及调用特定于路由的timeLog
中间件。
有两个类似的问题可以帮助你解决这个问题:
如何构建express.js应用程序?
Nodejs/Expressjs应用程序结构
- 尝试使用Node.js动态路由从IMDB中抓取电影内容.但是在我的output.json文件中没有定义
- 从javascript文件中获取JSON,并使用NodeJS在路由文件中显示
- 我应该在哪里放置用于角度路由的 html 文件
- 如何使用页面.js和 Apache 路由/提供静态文件
- 重新路由时重新加载.js文件
- 具有来自静态json文件的路由的主干应用程序(只读)
- 从expressjs路由中排除文件夹
- REST路由多个文件节点js
- express/如何将路由参数传递到其他文件
- 自定义 Mod 文件夹的角度路由问题
- 如何在 ember.js 中访问路由文件中的 :id 参数
- 侧边菜单运行,但路由文件不显示任何内容
- 当我在快速.js路由文件中使用 socket.io 时重复
- AngularJS路由不适用于子文件夹
- 余烬 - 如何将属性从路由文件传递到返回 401 的车把模板
- 如何找出哪个 JS 文件正在重新路由 URL
- 将文件上传到特定路由不起作用
- 如何从路由文件中的子文件夹设置控制器文件
- Ruby on Rails 路由前缀,而不是在 .erb.js 文件中进行预处理
- Symfony - 在外部JS文件中创建路由