Expressjs将动态大写和间隔路径路径转换为连字符和小写路径

时间:2014-10-10 12:21:32

标签: node.js mongodb express routes

我遇到了一个问题,我正在尝试使用"标题"来自我的mongoosedb文档的属性作为文档路径,但所有字符都是小写的,空格用连字符替换。我之前尝试使用过解决方案,但无法生成我的视图,而且Url包含大写字母和%20的空格。

以下是我当前的设置重点是'/blog/:blogpost_title'路线:

routes.js:

var express = require('express');
var router = express.Router();
var blogDB = require('../config/blogDB.js');
var Blogpost = require('./models/blogModel.js');
var paginate = require('express-paginate');

//index 
router.use(paginate.middleware(10, 50));




    router.route('/') 


        // START GET method
        .get(function(req, res, next) {

            Blogpost.paginate({}, req.query.page, req.query.limit, function(err, pageCount, blogpost, itemCount) {




                if (err) return next(err)

                        if (err)
                            res.send(err);


                        blogpost.title = req.body.title; // get the blog title
                        blogpost.author = req.body.author; // get the author name
                        blogpost.tagline = req.body.tagline; // get tagline
                        blogpost.content = req.body.content; // get the blog content
                        blogpost.category = req.body.category; // get the category
                        blogpost.tags = req.body.tags; // get the tags


                        res.format({
                            html: function() {
                                res.render('pages/index', {
                                    blogpost: blogpost,
                                    pageCount: pageCount,
                                    itemCount: itemCount
                                })
                            },
                            json: function() {

                                res.json({
                                    object: 'blogpost',
                                    has_more: paginate.hasNextPages(req)(pageCount),
                                    data: blogpost
                                })
                            }
                        }); // END res.format(html, json)
            }); // END Blogpost.paginate
        }); // END GET method

router.route('/admin/posts/create')

    // START POST method
        .post(function(req, res) {

            var blogpost = new Blogpost(); // create a new instance of a Blogpost model

            blogpost.title = req.body.title; // set the blog title
            blogpost.author = req.body.author; // set the author name
            blogpost.tagline = req.body.tagline; // set the tagline
            blogpost.content = req.body.content; // set the blog content
            blogpost.category = req.body.category; // set the category
            blogpost.tags = req.body.tags; // set the tags
                //Save Blog Post
                blogpost.save(function(err) {
                    if (err)
                        res.send(err);

                    res.redirect(303, '/'); //NEEDS TO BE CHANGED
                });

        }) // END POST method


        .get(function(req, res) {
            res.render('pages/blogpost-create');
        });

function getSearchCriteria(params) {
      return {
          title: params.blogpost_title
      };
}

function getBlogpostUpdate(body) {
      return {
           title: body.title,
           author: body.author,
           tagline: body.tagline,
           content: body.content,
           category: body.category,
           tags: body.tags
      };
}

var blogpostsRoute = router.route('/blog/:blogpost_title');

// to manipulate your route params, use router.param
router.param('blogpost_title', function (req, res, next, blogpost_title) {
    req.params.blogpost_title = blogpost_title.toLowerCase();
    next();
});

blogpostsRoute 
    .get(function (req, res) {
         var searchCriteria = getSearchCriteria(req.params);
         Blogpost.findOne(searchCriteria, function (err, blogpost) {
             if (err)
                 res.send(err);
             res.render('pages/blogpost', {
                 blogpost: blogpost
             })
         })
     })
     .put(function (req, res) {
         var searchCriteria = getSearchCriteria(req.params);
         var updated = getBlogpostUpdate(req.body)
         Blogpost.findOneAndUpdate(searchCriteria, updated, function (err, updated) {
             if (err)
                 res.send(err);


             res.json({ message: 'Blog updated.' });
         });
     })
     .delete(function (req, res) {
         var searchCriteria = getSearchCriteria(req.params);
         Blogpost.findOneAndRemove(searchCriteria, function (err, removed) {
             if (err)
                res.send(err);

             res.json({ message: 'Successfully deleted' });
         });
     });

//about
    router.get('/about', function(req, res) {
            res.render('pages/about');
    });

//resume
    router.get('/resume', function(req, res) {
            res.render('pages/resume');
    });



module.exports = router;

index.ejs:

<div class="grid">
    <div class="col-9-12">
        <div class="blog-content">
            <% blogpost.forEach(function(blogpost) { %>
                <tr>
                    <td><h2><a href="#" class="blog-title"><%= blogpost.title %></a></h2></td>
                    <td><h3><%= blogpost.date %></h3></td>
                    <td><h3 class="blog-category"><%= blogpost.category %></h3></td>
                    <td><h3 class="blog-tagline"><i><%= blogpost.tagline %></i></h3></td>
                    <td><p><%=: blogpost.content | truncate:800 | append:'...' %></p></td>
                    <td><a href="/blog/<%= blogpost.title %>" class="blog-read-more">Read More</a></td>
                </tr>
                <% }); %>
        </div>
    </div>

blogModel.js

var mongoose    = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var Schema      = mongoose.Schema;



var BlogPostSchema  = new Schema({
        title: String,
        author: String,
        tagline: String,
        category: String,
        content: String,
        tags: { type: String, lowercase: true },
        date: { type: Date, default: Date.now }
});

BlogPostSchema.plugin( mongoosePaginate );

var Blogpost = mongoose.model("Blogpost", BlogPostSchema);



module.exports = mongoose.model('Blogpost', BlogPostSchema);

0 个答案:

没有答案