Node.js:不同文件中的配置和路由

2024-04-20前端开发问题
10

本文介绍了Node.js:不同文件中的配置和路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在启动一个新的 Node.js 应用程序,这一次,我试图正确地组织代码,而不是将所有内容都放在同一个文件中.

I am starting a new Node.js app and this time, I'm trying to organize the code correctly instead of having everything in the same file.

我现在在 server.coffee 上只有一个简单的设置:

I only have a simple setup now at 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

我对这个简单的代码有一些疑问,我知道所有答案都取决于开发人员,但我真的想把它做对:

I have some questions regarding that simple code and I know that all the answers depend on the developer but I really want to do it right:

  • server.js 文件应该比 app.listen 多吗?究竟应该有什么?
  • 不应该所有配置都在与路由不同的文件中吗?如何在运行 server.coffee 时将 app.get 删除到其他文件并使其工作?
  • 我在 Hubot 等许多应用程序中看到的 index.coffee 究竟应该包含什么?
  • Should the server.js file have more than the app.listen? What should be there exactly?
  • Shouldn't all the configurations be in a different file than the routes? How can I remove the app.get to other file and make them work when I run the server.coffee?
  • What exactly should contain the index.coffee that I see in a lot of apps like Hubot?

我希望有人能给我一个取决于"以外的答案.

I hope someone can give me an answer other than "it depends".

推荐答案

您可以利用 require,只需将 app var 作为参数传递给方法.它不是最漂亮的语法,在 CoffeeScript 中也不是,但你应该明白.

You can leverage require, and simply pass the app var in as a parameter to a method. It's not the prettiest syntax, nor is it in CoffeeScript, but you should get the idea.

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 等)

If you want to take it a step further, I separate my routes into smaller groups, and in it's own subfolder. (like: routes/auth.js for login/logout, routes/main.js for home/about/contact and so on)

// 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,源本身保持不变)

(rename routes.js from before as routes/main.js, the source itself remains the same)

这篇关于Node.js:不同文件中的配置和路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5