无法获取未定义node.js的方法get

Cannot get method get of undefined node.js

本文关键字:方法 get js node 获取 未定义      更新时间:2023-09-26

我正在为一个express应用程序编写一种新的结构和可伸缩性。

问题:

我不喜欢在app.js文件中定义每个路由。

解决方案:

使某些内容自动化,以便自动加载路线。

到目前为止,我在index.js(路由文件夹)文件上有以下代码:

var fs = require('fs');
module.exports = function(app) {
  recursive_require(__dirname, app);
  function recursive_require(directory, app) {
    fs.readdirSync(directory).forEach(function(file) {
      if (fs.lstatSync(directory + '/' + file + '/').isDirectory()) {
        var has_no_js_files = false;
        directory = directory + '/' + file + '/'; 
        console.log('Scanning recursively on ' + directory);
        // We run across the directory to check if there are any js files.
        fs.readdirSync(directory).forEach(function(file) {
          console.log('Reading file/directory ' + file);
          if (file.match(/'.js$/g)) {
            has_no_js_files = true;
            console.log('Found js files on directory ' + directory);
          }
        });
        // If the folder has no js files, we take in mind that there are other folders inside
        // so we scan the folder recursively.
        if (!has_no_js_files) {
          console.log('No JS files found on ' + directory + ' going to scan recursively');
          recursive_require(directory.substr(0, directory.lastIndexOf('/')));
        } else {
          // Otherwise, we require the directory taking in mind that we have just js files.
          console.log('Found JS files on ' + directory + ', require them');
          require(directory)(app);
        }
      }
    });
  }
}

现在,这似乎有效,但我有点问题。。

我的想法是把所有东西都放在文件夹上,比如说,这个结构:

routes
  admin
    posts
      index.js <- handles add, remove, edit for posts
    users
      index.js <- handles add, remove, edit for users
  blog
    posts
      index.js <- handles show for frontend
  index.js <- Loads all of the files recursively.

现在,我对这个代码有点问题。。。

我有这个错误:

PS C:'Users'bony-_000'Documents'GitHub'node-blog> node app
Scanning recursively on C:'Users'bony-_000'Documents'GitHub'node-blog'routes/admin/
Reading file/directory posts
No JS files found on C:'Users'bony-_000'Documents'GitHub'node-blog'routes/admin/ going to scan recursively
Scanning recursively on C:'Users'bony-_000'Documents'GitHub'node-blog'routes/admin/posts/
Reading file/directory index.js
Found js files on directory C:'Users'bony-_000'Documents'GitHub'node-blog'routes/admin/posts/
Found JS files on C:'Users'bony-_000'Documents'GitHub'node-blog'routes/admin/posts/, require them
C:'Users'bony-_000'Documents'GitHub'node-blog'routes'admin'posts'index.js:2
  app.get('/admin/posts/add', function(req, res) {
      ^
TypeError: Cannot call method 'get' of undefined

虽然我正在发送应用程序变体…

任何帮助都将不胜感激,也请随时使用代码。

我已经解决了所有的问题,现在我有了结构化的MVC Online!

这是任何人都可以使用的代码:

var fs = require('fs'),
    required_files = [];

module.exports = function(app) {
  recursive_require(__dirname, __dirname, app);
  function recursive_require(directory, base_dir, app) {
    fs.readdirSync(directory).forEach(function (input) {
      var next_directory = directory + '/' + input + '/';
      // If we are on the base dir, we ignore the index.js file
      if (!(required_files.indexOf(base_dir + '/index') > -1)) {
        required_files.push(base_dir + '/index');  
      }
      // Check if it's a directory
      if (fs.lstatSync(next_directory).isDirectory()) {
        // We require it recursively
        console.log('Reading directory ' + next_directory);
        recursive_require(next_directory.substr(0, next_directory.lastIndexOf('/')), base_dir, app);
      } else {
        // We require all (except the index.js file if the var is set to true) js files on folder
        require_files(directory, app);
        return;
      }
    });
  }
  function require_files(directory, app) {
    fs.readdir(directory, function(err, files) {
      files.forEach(function(file) {
        if (file.match(/'.js$/g)) {
          var file_path = directory + '/' + file;
          file_path = file_path.substr(0, file_path.indexOf('.js'));
          if (required_files.indexOf(file_path) == -1) {
            required_files.push(file_path);
            require(file_path)(app);
          }
        }
      });
    });
    return;
  }
}

欢迎提出任何建议。

相关文章: