Express应用程序要求脚本标签位于错误的位置

时间:2015-10-13 17:13:19

标签: node.js express requirejs pug

基本上,在http://localhost:3000/,一切正常,但当我转到http://localhost:3000/post/23时,我在控制台中收到以下错误。

require.js:1 Uncaught SyntaxError: Unexpected token <

在网络标签中检查这一点,我注意到我的客户端在尝试加载require时会在http://localhost:3000/post/require.js加载它,然后返回我的模板的html。请注意,http://localhost:3000/require.js工作正常,但在访问我的/post网址时,我无法通过该网址获取此信息。

<!DOCTYPE html><html lang="en"><head><title>PodShuff</title><script data-main="requireconfig.js" src="require.js"></script><link href="normalize.css/normalize.css" rel="stylesheet"><link href="style.css" rel="stylesheet"></head><body><button id="theBuddon">New</button><h2>Title Here</h2><h4>Author Here</h4><p>Post Body Here</p></body></html>

显然,问题的一部分是,当我真的希望在根网址上需要它时,在/ post时加载requirejs,但设置基本网址似乎不起作用。另外,为什么世界上它需要html模板而不只是说“找不到”?我已经在下面包含了我的requireconfig和代码的相关部分。如何停止此控制台错误并要求require.js正确的方法?

requireconfig     'use strict';

require.config({
    paths: {
            faker: 'node_modules/faker/build/build/faker',
            text: './text',
            underscore: 'node_modules/underscore/underscore',
            jquery: 'node_modules/jquery/dist/jquery',
            Backbone: 'node_modules/backbone/backbone',
            Post: '/public/scripts/post',
            PostCollection: '/public/scripts/post-collection',
            PostView: '/public/scripts/post-view',
            PostCollectionView: '/public/scripts/post-collection-view',
            main: '/public/scripts/main',
            dotenv: '/node_modules/dotenv/config',
            fakeData: 'public/scripts/fake-data',
        },
        baseUrl: "/"
});

require([
        'PostView',
        'Post',
        'PostCollection',
        'main',
]);

服务器

var app = express();
app.use(express.static(path.join(__dirname)));
app.use(express.static(path.join(__dirname, "node_modules")));
app.use(express.static(path.join(__dirname, "node_modules/requirejs")));

app.use(express.static(path.join(__dirname, "public/views")));
app.use(express.static(path.join(__dirname, "public/views/templates")));
app.use(express.static(path.join(__dirname, "public/scripts")));
app.use(express.static(path.join(__dirname, "public/stylesheets")));
app.use(bodyParser.json());
bodyParser.urlencoded({extended:false});

app.set("view engine", "jade");

app.listen("3000", function(){
    console.log("Listening on port 3000");
});

app.get("/", function(req, res){
    res.render("index");
});

app.get("/post/:id", function(req, res){
    var rawPost = PostSchema.find({postId: req.params.id}, function(err, posts){
        var post = new Post(rawPost)
        res.render('single-post', {model: post});
    });
});

玉石模板:

 //layout.jade
    doctype html
    html(lang="en")
        head
            title="PodShuff"
            script(data-main='requireconfig.js', src="require.js")
            link(href="normalize.css/normalize.css", rel="stylesheet")
            link(href="style.css", rel="stylesheet")
        body
            button(id="theBuddon")="New"
            block cont

0 个答案:

没有答案