无法获取未定义node.js的方法get
Cannot get method get of undefined node.js
我正在为一个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;
}
}
欢迎提出任何建议。
相关文章:
- 有没有一种方法可以防止img get请求使用css或js发生
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 如何在router.get()方法中传递url作为参数
- Get方法获取csv文件的内容
- 如何使用get或post方法连接和执行ajax
- 修复AngularJS错误:提供程序必须从$get工厂方法返回值
- JQuery AJAX - 如何使用方法 GET 调用刷新/重新加载页面
- 无法获取未定义node.js的方法get
- WCF json POST请求方法:GET和405(方法不允许)
- 使用表单上的页面锚点提交方法=get
- 从日期选取器发送日期参数,在方法 GET 上不带“/”
- AngularJS根据方法(Get,Put)更改URL
- “无法调用未定义的方法'get'”在Chrome扩展弹窗中.js
- 使用锚标记在 nodeJS 中覆盖方法 GET 以删除
- 不能调用方法'get'未定义的使用mongodb与express
- 为什么javascript触发提交表单(方法get,目标空白)打开弹出窗口而不是新选项卡
- Rails链接到忽略e.preventDefault();从jQuery调用,因为方法::get已设置
- Emberjs Uncatch TypeError:Object #
- 不允许使用OPTIONS方法.Get工作,但不是POST
- 将用户ID传递给核心ajax方法GET,以检索谷歌云存储中的数据