将Node App划分为不同的文件 [英] Divide Node App in different files

查看:116
本文介绍了将Node App划分为不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Socket.IO开发我的第一个Node.js应用程序,一切都很好,但是现在该应用程序正在逐渐变大,我想将应用程序代码划分为不同的文件,以进行更好的维护.

I'm developing my first Node.js App with Socket.IO and everything is fine but now the app is slowly getting bigger and I'd like to divide the app-code into different files for better maintenance.

例如,我要在主文件中定义所有猫鼬模式和路由.下面是用于socket.IO连接的所有功能.但是,现在我想为架构提供一个额外的文件,为路由提供一个额外的文件,为功能提供一个额外的文件.

For example I'm defining all my mongoose schemas and the routings in the main file. Underneath are all the functions for the socket.IO connection. But now I want to have an extra file for the schemas, an extra file for routing and one for the functions.

当然,我知道可以编写自己的模块或使用require加载文件的可能性.这对我来说毫无意义,因为如果不将它们设置为全局变量,我将无法使用像app,io或db这样的var.如果将它们传递给模块中的函数,则无法更改它们.我想念什么?我想看一个例子,在不使用全局变量的情况下如何做到这一点.

Of course, I'm aware of the possibility to write my own module or load a file with require. That just does not make sense for me, because I can't work with the vars like app, io or db without making them global. And if I pass them to a function in my module, I can't change them. What am I missing? I'd like to see an example how this is done in practice without using global vars..

推荐答案

听起来您有 OO设计原理可能会在这里有所帮助.

It sounds like you have a highly coupled application; it's difficult for you to split out your code into modules because pieces of the application that should not depend on each other do. Looking into the principles of OO design may help out here.

例如,如果您要将数据删除逻辑从主应用程序中分离出来,则应该能够这样做,因为数据库逻辑不应依赖于appio –它应该能够单独工作,然后将其require放入应用程序的其他部分即可使用.

For example, if you were to split your dataabse logic out of the main application, you should be able to do so, as the database logic should not depend on app or io--it should be able to work on its own, and you require it into other pieces of your application to use it.

这是一个相当基本的示例-比实际代码更多的伪代码,因为重点是通过示例演示模块化,而不是编写有效的应用程序.这只是您决定构建应用程序的众多方式中的一种.

Here's a fairly basic example--it's more pseudocode than actual code, as the point is to demonstrate modularity by example, not to write a working application. It's also only one of many, many ways you may decide to structure your application.

// =============================
// db.js

var mongoose = require('mongoose');
mongoose.connect(/* ... */);

module.exports = {
  User: require('./models/user');
  OtherModel: require('./models/other_model');
};


// =============================
// models/user.js (similar for models/other_model.js)

var mongoose = require('mongoose');
var User = new mongoose.Schema({ /* ... */ });
module.exports = mongoose.model('User', User);


// =============================
// routes.js

var db = require('./db');
var User = db.User;
var OtherModel = db.OtherModel;

// This module exports a function, which we call call with
// our Express application and Socket.IO server as arguments
// so that we can access them if we need them.
module.exports = function(app, io) {
  app.get('/', function(req, res) {
    // home page logic ...
  });

  app.post('/users/:id', function(req, res) {
    User.create(/* ... */);
  });
};


// =============================
// realtime.js

var db = require('./db');
var OtherModel = db.OtherModel;

module.exports = function(io) {
  io.sockets.on('connection', function(socket) {
    socket.on('someEvent', function() {
      OtherModel.find(/* ... */);
    });
  });
};


// =============================
// application.js

var express = require('express');
var sio = require('socket.io');
var routes = require('./routes');
var realtime = require('./realtime');

var app = express();
var server = http.createServer(app);
var io = sio.listen(server);

// all your app.use() and app.configure() here...

// Load in the routes by calling the function we
// exported in routes.js
routes(app, io);
// Similarly with our realtime module.
realtime(io);

server.listen(8080);

这一切都花在了我头上,而对各种API的文档的检查却很少,但是我希望它能为您从应用程序中提取模块的方法打下基础.

This was all written off the top of my head with minimal checking of the documentation for various APIs, but I hope it plants the seeds of how you might go about extracting modules from your application.

这篇关于将Node App划分为不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆