在不同的文件中划分节点应用程序

时间:2023-01-24 12:55:40

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开发我的第一个Node.js应用程序,一切都很好,但现在应用程序正在慢慢变大,我想将应用程序代码分成不同的文件以便更好地维护。

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.

例如,我正在定义所有我的mongoose模式和主文件中的路由。下面是socket.IO连接的所有功能。但是现在我想为模式添加一个额外的文件,一个用于路由的额外文件和一个用于函数的文件。

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

当然,我知道可以编写自己的模块或加载带有require的文件。这对我来说没有意义,因为我无法使用像app,io或db这样的vars而不会让它们变得全局化。如果我将它们传递给我的模块中的函数,我就无法改变它们。我错过了什么?我想看一个例子,如何在不使用全局变量的情况下实现这一点。

1 个解决方案

#1


14  

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.

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

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.

例如,如果您要将数据库逻辑从主应用程序中分离出来,您应该能够这样做,因为数据库逻辑不应该依赖于应用程序或io - 它应该能够独立工作,而您要求它进入你的应用程序的其他部分使用它。

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

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.

这些都是在我的脑海中写下来的,只需要对各种API的文档进行最少的检查,但我希望它能够为您从应用程序中提取模块提供种子。

#1


14  

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.

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

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.

例如,如果您要将数据库逻辑从主应用程序中分离出来,您应该能够这样做,因为数据库逻辑不应该依赖于应用程序或io - 它应该能够独立工作,而您要求它进入你的应用程序的其他部分使用它。

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

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.

这些都是在我的脑海中写下来的,只需要对各种API的文档进行最少的检查,但我希望它能够为您从应用程序中提取模块提供种子。