我想将我的路由分成不同的文件,其中一个文件包含所有路由,另一个文件包含相应的操作.我目前有一个解决方案来实现这一点,但是我需要让app-instance全局能够在操作中访问它.我目前的设置如下:
app.js:
var express = require('express');
var app = express.createServer();
var routes = require('./routes');
var controllers = require('./controllers');
routes.setup(app, controllers);
app.listen(3000, function() {
console.log('Application is listening on port 3000');
});
Run Code Online (Sandbox Code Playgroud)
routes.js:
exports.setup = function(app, controllers) {
app.get('/', controllers.index);
app.get('/posts', controllers.posts.index);
app.get('/posts/:post', controllers.posts.show);
// etc.
};
Run Code Online (Sandbox Code Playgroud)
控制器/ index.js:
exports.posts = require('./posts');
exports.index = function(req, res) {
// code
};
Run Code Online (Sandbox Code Playgroud)
控制器/ posts.js:
exports.index = function(req, res) {
// code
};
exports.show = function(req, res) {
// code
};
Run Code Online (Sandbox Code Playgroud)
但是,这个设置有一个很大的问题:我有一个数据库 - 我需要传递给操作的app-instance(controllers/*.js).我能想到的唯一选择是将两个变量都变为全局变量,而这并非真正的解决方案.我想将路线与行动分开,因为我有很多路线,并希望它们位于中心位置.
将变量传递给操作但将操作与路由分开的最佳方法是什么?
我目前正在设计一个API,我遇到了一个小问题: 当你应该能够通过ID或slug来识别项目时,RESTful API的URL应该怎么样?
我可以想到三个选择:
GET /items/<id>
GET /items/<slug>
Run Code Online (Sandbox Code Playgroud)
这要求slu and和ID是可区分的,在这种情况下不一定给出.我想不出这个问题的干净解决方案,除非你做这样的事情:
GET /items/id/<id>
GET /items/slug/<slug>
Run Code Online (Sandbox Code Playgroud)
这样可以正常工作,但是这不是我想要通过slug或ID来识别项目的唯一地方,当想要为其他操作实现相同的方法时,它很快会变得非常难看.它只是不太可扩展,这导致我们采用这种方法:
GET /items?id=<id>
GET /items?slug=<slug>
Run Code Online (Sandbox Code Playgroud)
这似乎是一个很好的解决方案,但我不知道它是否是人们期望的,因此它可能导致由于错误使用而导致的令人沮丧的错误.而且,为这个实现路由并不是那么容易 - 或者说是干净利落的.但是,它很容易扩展,看起来非常类似于获取多个项目的方法:
GET /items?ids=<id:1>,<id:2>,<id:3>
GET /items?slugs=<slug:1>,<slug:2>,<slug:3>
Run Code Online (Sandbox Code Playgroud)
但这也有一个缺点:如果有人想要识别他想用ID获取的一些物品,但其他人有一个slu ?? 混合这些标识符并不容易实现.
对于这些问题,最好和最广泛接受的解决方案是什么?一般来说,在设计这样的API时重要的是什么?
我在Mac OS X上使用Chrome 12,我在文档中包含了jQuery 1.6.1.
我尝试将文件的内容作为文本读取,并使用以下函数将其保存在数据对象中:
this.upload = function(file) {
console.log('FileHandler.upload called with ' + file.name + '.');
console.log(file);
console.log(this.reader);
data = {
content: this.reader.readAsText(file)
}
console.log('Content: ' + data.content);
}
Run Code Online (Sandbox Code Playgroud)
"file"接缝是有效的文件对象,"this.reader"是FileReader类型的新实例.此代码创建以下控制台输出:
http://cl.ly/1Y2b383G2F272x1m1P0N

问题解决了,看了评论
我在HTML5文件API中遇到的第三个问题:我仍在Mac OS X Snow Leopard上使用Chrome 12,我仍在尝试使用HTML5文件API读取文件,但因为"SECURITY_ERR"而调用了FileHandler.error() occurres.我尝试读取的文件是来自桌面的常规.txt文件,但它既不能用于其他文件,尽管我可以使用常规应用程序打开它们.
function FileHandler(files, action) {
console.log('FileHandler called.');
this.files = files;
this.reader = new FileReader();
this.action = action;
this.handle = function() {
console.log('FileHandler.handle called.');
for (var i = 0; i < this.files.length; i++) {
this.reader.readAsDataURL(files[i]);
}
}
this.upload = function() {
console.log('FileHandler.upload called.');
console.log(this.reader);
data = {
content: this.reader.result
}
console.log(data);
}
this.error = function() {
console.log('An error occurred while reading the file.');
console.log(this.reader.error);
}
this.reader.onload = this.upload.bind(this);
this.reader.onerror = this.error.bind(this);
}
Run Code Online (Sandbox Code Playgroud)
该代码生成以下控制台输出:http …
fileapi ×2
filereader ×2
html5 ×2
javascript ×2
api ×1
api-design ×1
express ×1
node.js ×1
rest ×1