EJS文件尽管从模板部分提供,但不渲染样式表

时间:2014-09-26 12:32:39

标签: javascript node.js express ejs

我遇到了一个奇怪的问题,我的页面被路由以从我的主页上的数据库列表中提供有关单个记录的其他信息,而不是拉入我的partial / head中的样式表,而是传递对象信息正确。我不确定为什么会这样。不接受我的样式信息的具体路线是/ blog /:blog_id。

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 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
            blogpost.date = req.body.date; // set the date of the post
                //Save Blog Post
                blogpost.save(function(err) {
                    if (err)
                        res.send(err);

                    res.json({ message: 'Blog created.' });
                });

        }) // END POST method


        // 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
                        blogpost.date = req.body.date; // get the date of the post

                        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


    //Route for individual blogs
    router.route('/blog/:blogpost_id')


    // START GET method blog by ID  
    .get(function(req, res) {


        Blogpost.findById(req.params.blogpost_id, function(err, blogpost) {
            if (err)
                res.send(err);


            //res.json(blogpost);
            res.render('pages/blogpost', {
                blogpost: blogpost
            });
        });
    }) // END GET method blog by ID

    // START PUT method
    .put(function(req, res) {

        Blogpost.findById(req.params.blogpost_id, function(err, blogpost) {

            if (err)
                res.send(err);


            blogpost.title = req.body.title; // update the blog title
            blogpost.author = req.body.author; // update the author name
            blogpost.tagline = req.body.tagline; // update the tagline
            blogpost.content = req.body.content; // update the blog content
            blogpost.category = req.body.category; // update the category 
            blogpost.tags = req.body.tags; //update the tags
            blogpost.date = req.body.date; // update the date of the post


            blogpost.save(function(err) {
                if (err)
                    res.send(err);


                res.json({ message: 'Blog updated.' });
            });

        });

    }) // END PUT method

    // START DELETE method
    .delete(function(req, res) {

        Blogpost.remove({
            _id: req.params.blogpost_id

        }, function(err, bear) {
            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;

部分/ head.ejs:

<title>Page - Title | Example</title>

<link rel="stylesheet" type="text/css" href="stylesheets/simplegrid.css">
<link rel="stylesheet" type="text/css" href="stylesheets/styles.css">

页/ blogpost.ejs:

<html>
<head>
    <% include ../partials/head %>
</head>

<body>

    <header>
        <% include ../partials/header %>
    </header>

    <div class="grid">
        <div class="col-1-1">
            <div class="blog-content">
                <h1><%= blogpost.title %></h1>
            </div>
        </div>
    </div>


    <footer>
        <% include ../partials/footer %>
    </footer>

</body>
</html>

比较图片:

除了/ blog /:blog_id

之外,在生成我的任何路线时找到的样式

Style on every page.

生成我的/ blog /:blog_id路径时找到的样式。

No style found for individual blogposts

1 个答案:

答案 0 :(得分:0)

您需要在/

之前添加样式表

想象一下,您访问了网址localhost/blog/test,浏览器将在localhost/blog/test/stylesheets/styles.css寻找样式表。

通过添加/您从根目录提供服务localhost/stylesheets/styles.css