在express中root后通过可选参数传递路由控制?
我正在开发一个简单的网址缩短应用程序,并具有以下快速路线:
app.get('/', function(req, res){
res.render('index', {
link: null
});
});
app.post('/', function(req, res){
function makeRandom(){
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 3 /*y u looking at me <33??*/; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
var url = req.body.user.url;
var key = makeRandom();
client.set(key, url);
var link = 'http://50.22.248.74/l/' + key;
res.render('index', {
link: link
});
console.log(url);
console.log(key);
});
app.get('/l/:key', function(req, res){
client.get(req.params.key, function(err, reply){
if(client.get(reply)){
res.redirect(reply);
}
else{
res.render('index', {
link: null
});
}
});
});
我想从我的路线中删除 /l/
(以使我的网址更短)并设置 :key 参数选修的。这是执行此操作的正确方法吗:
app.get('/:key?', function(req, res, next){
client.get(req.params.key, function(err, reply){
if(client.get(reply)){
res.redirect(reply);
}
else{
next();
}
});
});
app.get('/', function(req, res){
res.render('index, {
link: null
});
});
不确定我是否需要指定我的 /
路线是“下一个”路线。但由于我唯一的其他路线是更新后的 /
路线,我想它会工作得很好。
I'm working on a simple url-shortening app and have the following express routes:
app.get('/', function(req, res){
res.render('index', {
link: null
});
});
app.post('/', function(req, res){
function makeRandom(){
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 3 /*y u looking at me <33??*/; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
var url = req.body.user.url;
var key = makeRandom();
client.set(key, url);
var link = 'http://50.22.248.74/l/' + key;
res.render('index', {
link: link
});
console.log(url);
console.log(key);
});
app.get('/l/:key', function(req, res){
client.get(req.params.key, function(err, reply){
if(client.get(reply)){
res.redirect(reply);
}
else{
res.render('index', {
link: null
});
}
});
});
I would like to remove the /l/
from my route (to make my url's shorter) and make the :key parameter optional. Would this be the correct way to do this:
app.get('/:key?', function(req, res, next){
client.get(req.params.key, function(err, reply){
if(client.get(reply)){
res.redirect(reply);
}
else{
next();
}
});
});
app.get('/', function(req, res){
res.render('index, {
link: null
});
});
Not sure if I need to specify that my /
route is the one to be "nexted" to. But since my only other route would be my updated post /
route, I would imagine it would work fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将取决于 client.get 在将 undefined 作为其第一个参数传递时执行的操作。
像这样的事情会更安全:
在回调中调用 next() 没有问题。
根据 this,处理程序按照添加的顺序调用,因此只要你的下一个路由是 app.get('/', ...) 如果没有键,它就会被调用。
That would work depending on what client.get does when passed undefined as its first parameter.
Something like this would be safer:
There's no problem in calling next() inside the callback.
According to this, handlers are invoked in the order that they are added, so as long as your next route is app.get('/', ...) it will be called if there is no key.
Express版本:
可选参数非常方便,您可以使用express轻松声明和使用它们:
在上面的代码中,batchNo是可选的。 Express 会将其视为可选,因为在 URL 构造之后,我给出了一个“?” batchNo '/:batchNo?' 之后的符号
现在我可以仅使用categoryId 和productId 进行调用,或者使用所有三个参数进行调用。
Express version:
Optional parameter are very much handy, you can declare and use them easily using express:
In the above code batchNo is optional. Express will count it optional because after in URL construction, I gave a '?' symbol after batchNo '/:batchNo?'
Now I can call with only categoryId and productId or with all three-parameter.