【发布时间】:2015-11-07 14:19:33
【问题描述】:
所以我在 nodejs api 中有以下猫鼬模式:
var profileschema = new mongoose.Schema({
name: { type: String },
surname: { type: String },
id: { type: String }
});
还有以下路线:
var profile = express.Router();
profile.route('/profile')
.get(profilecrtl.findAllProfile)
.post(profilecrtl.addProfile);
我可以制作其他路线,例如 /profile/:id,它们可以完美运行。
但我想根据用户在同一方法上询问和想要的参数生成自定义 URL,而不必对每种情况进行编码。例如:
- /profile?id=1234 应该给我关于 id=1234 个人资料的完整信息
{
id: '1234',
name: 'john'
surname: 'wicked'
}
- /profile?id=1234&name=john 应该给我和以前一样的完整个人资料
{
id: '1234',
name: 'john'
surname: 'wicked'
}
- /profile?id=1234&fields=name 应该只给我 id=1234 配置文件的名称
{
name: 'john'
}
在相同情况中是否有任何稳健的方法可以做到这一点,以便在未来发生任何变化时轻松扩展?
【问题讨论】:
标签: javascript node.js rest express