我为我的index.html 文件创建了一个句柄,以便当用户访问 http://localhost:3000/ 时[root] 它显示了我的index.html 的动态版本。但是,当页面加载时,除了我包含的部分内容之外,所有内容都会加载?
这是我放置部分的页面 handle 部分(newView.handlebar):
.
.
<main class="twit-container">
{{#each twitData}} //will iterate/insert data for the amount of objects in the array being passed in server.js
{{> newTwit}} //the name of my partial template
{{/each}}
</main>
.
.etc...
下面是我的 server.js 代码(我认为问题可能出在其中):
var path = require('path');
var express = require('express');
var exphbs = require('express-handlebars');
var fs = require('fs');
var app = express();
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
var port = process.env.PORT || 3000;
var rawData = fs.readFileSync('./twitData.json');
var twitData = JSON.parse(rawData); //get the tweet data (text/authors)
app.get('/', function (req, res) {
res.status(200).render('newView', {data: twitData});
});
app.use(express.static('public'));
app.get('*', function (req, res) {
res.status(404).sendFile(path.join(__dirname, 'public', '404.html'));
});
app.listen(port, function () {
console.log("== Server is listening on port", port);
});
最佳答案
您正在迭代 twitData
,但您从未将其发送到 View 。
<main class="twit-container">
{{#each twitData}} //will iterate/insert data for the amount of objects in the array being passed in server.js
{{> newTwit}} //the name of my partial template
{{/each}}
</main>
您需要这样发送:
res.status(200).render('newView', { twitData });
否则您将需要使用:{{#each data}}
。
关于javascript - 部分内容未在动态网页上呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50521185/