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

查看:20
本文介绍了将 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 等变量而不将它们设为全局变量.如果我将它们传递给模块中的函数,则无法更改它们.我错过了什么?我想看一个例子,如何在不使用全局变量的情况下在实践中做到这一点..

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..

推荐答案

听起来你有一个 高耦合 应用程序;您很难将代码拆分为模块,因为不应该相互依赖的应用程序部分会这样做.研究面向对象设计的原则可能会有所帮助.

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--它应该能够独立工作,并且您要求将其放入应用程序的其他部分以使用它.

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天全站免登陆