2014-02-08 36 views
0

更新:請參閱我的回答如下的最終解決方案使用對象:標題URL路徑,而不是:ID

我有一個獨特的標題名稱的對象,我想利用冠軍,而而不是整個網站的路線ID。

routes.js

// Current routing 

var articles = require('../app/controllers/articles') 

app.param('articleId', articles.load) 
app.get('/articles', articles.index) 
app.get('/articles/new', articles.new) 
app.post('/articles', articles.create) 
app.get('/articles/:articleId', articles.show) 
app.get('/articles/:articleId/edit', articles.edit) 
app.put('/articles/:articleId', articles.update) 
app.del('/articles/:articleId', articles.destroy) 

*** 

// Desired routing 

app.param('articleTitle', articles.load) 
app.get('/articles', articles.index) 
app.get('/articles/new', articles.new) 
app.post('/articles', articles.create) 
app.get('/articles/:articleTitle', articles.show) 
app.get('/articles/:articleTitle/edit', articles.edit) 
app.put('/articles/:articleTitle', articles.update) 
app.del('/articles/:articleTitle', articles.destroy) 

articles.js(控制器)

exports.load = function(req, res, next, title){ 
    Article.load(title, function (err, article) { 
    if (err) return next(err) 
    if (!article) return next(new Error('not found')) 
    req.article = article 
    next() 
    }) 
} 

exports.show = function(req, res){ 
    res.render('articles/show', { 
    title: req.article.title, 
    article: req.article 
    }) 
} 

article.js(型號)

ArticleSchema.statics = { 
    load: function (title, cb) { 
    this.findOne({ title : title }) 
     .exec(cb) 
    } 
} 

乙基本上,我已經推出了任何id引用,並將其替換爲標題引用。出於某種原因,他們根本不工作。

一個例子是將一個URL http://localhost:3000/articles/test而不是http://localhost:3000/articles/5TheId234,這是不行的,拋出此錯誤:GET /用品/測試404和148ms

提前感謝!

+1

你一定有一個'article'文檔中的蒙戈數據庫標題設置爲'test'?我懷疑這是問題:'Article.load'沒有找到任何文檔,也沒有調用導致404錯誤的next()。 – Tom

+0

你是對的,畢竟是數據。這是一箇舊的記錄使用舊的字段名稱(mongodb),所以它沒有一個標題,直到我更新它!我添加了一些額外的功能,作爲對我自己的問題的迴應,其中包括一個新的「url」字段,而不是標題。 –

回答

1

這很可能是一個複製和粘貼錯誤,當你張貼了這個問題,但我注意到,你的新路線有一個錯字(應該是articles.new):

app.get('/articles/new', rticles.new) 

我不知道它是否WASN也許是路線的排序會導致一些路線覆蓋其他路線。但我創建了一個簡單的測試,它似乎工作正常。

正如您所看到的,您可以看到以下測試使用了相同的路線,並且其工作方式與預期完全相同。點燃這個並看看?也許這可以縮小你的問題。

var express = require('express'); 
var app = express(); 


var articles = [ 
    {id: 1, title: 'tagtreetv_rocks', 
     link: 'http://localhost:3000/articles/tagtreetv_rocks' 
    }, 
    {id: 2, title: 'tagtreetv_launches', 
     link: 'http://localhost:3000/articles/tagtreetv_launches' 
    } 
]; 


app.param('articleTitle', function(req, res, next, title){ 
    var article = articles.filter(function(a){return a.title == title;}); 
    if (!article) return next(new Error('not found')); 
    req.article = article; 
    next(); 
}); 

app.get('/articles/new', function(req, res){ 
    res.json({ 
     id: '?', 
     title: '?' 
    }); 
}); 

app.get('/articles', function(req, res){ 
    res.json(articles); 
}); 

app.post('/articles', function(req, res){ 
    articles.push(req.body); 
    res.send(200); 
}); 

app.get('/articles/:articleTitle', function(req, res){ 
    res.json(req.article); 
}); 

app.get('/articles/:articleTitle/edit', function(req, res){ 
    res.json(req.article); 
}); 



app.listen(3000); 
console.log('Listening on port 3000'); 

順便說一句,如果你有興趣完整的堆棧JavaScript編程,我最近推出的一些截屏的話題:http://tagtree.tv

+0

啊,這是一個複製和粘貼錯誤,我解決了它。所以猜猜看,究竟是數據。這是一箇舊的記錄使用舊的字段名稱(mongodb),所以它沒有一個標題,直到我更新它!至於你的網站,它看起來非常好,希望我明天可以看到他們中的一些人。 –

+0

甜蜜,非常感謝反饋:) – hendrikswan

0

這是一個數據錯誤,但我已經通過創建和利用基於掀起了獨立URL字段中輸入標題對我的初始結構改善:

routes.js

var articles = require('../app/controllers/articles') 

app.param('articleURL', articles.load) 
app.get('/articles', articles.index) 
app.get('/articles/new', articles.new) 
app.post('/articles', articles.create) 
app.get('/articles/:articleURL', articles.show) 
app.get('/articles/:articleURL/edit', articles.edit) 
app.put('/articles/:articleURL', articles.update) 
app.del('/articles/:articleURL', articles.destroy) 

articles.js(控制器)

exports.create = function (req, res) { 
    var article = new Article(req.body) 

    // Builds URL by removing spaces 
    // note: url is a hidden field in the form 
    // replaces spaces with underscores 
    // removes all special characters (not '_') 
    // converts to lowercase 
    // to shorten the length, use 'substring(0,18)' - 18 char max example 
    article.url = sport.title.replace(/ /g,'_').replace(/\W/g,'').toLowerCase() 

    article.uploadAndSave(req.files.image, function (err) { 
    if (!err) { 
     req.flash('success', 'Successfully created article!') 
     return res.redirect('/article/'+article.url) 
    } 
    res.render('articles/new', { 
     title: 'New Article', 
     sport: article, 
     errors: utils.errors(err.errors || err) 
    }) 
    }) 
} 

exports.load = function(req, res, next, title){ 
    Article.load(title, function (err, article) { 
    if (err) return next(err) 
    if (!article) return next(new Error('not found')) 
    req.article = article 
    next() 
    }) 
} 

exports.show = function(req, res){ 
    res.render('articles/show', { 
    title: req.article.title, 
    article: req.article 
    }) 
} 

article.js(型號)

ArticleSchema.statics = { 
    load: function (url, cb) { 
    this.findOne({ url : url }) 
     .exec(cb) 
    } 
} 
相關問題