use*_*467 5 html javascript node.js express
如何以 html 形式显示服务器端 Node.js 变量中保存的文本。
换句话说,为了更加清楚,我从访问 twitter (Twitt) 的 api 中提取了输出,并且我试图在页面底部显示该文本。
网页的名称是这样的
app.get('/', function (req, res) {
res.sendfile('./Homepage.html');
});
Run Code Online (Sandbox Code Playgroud)
这是调用 Node.js 文件的地方:
var express = require('express');
var app = express();
Run Code Online (Sandbox Code Playgroud)
这是变量的位置:
T.get('search/tweets', { q: hash1 + ' since:2013-11-11', count: 5 },
function(err, reply) {
response = (reply),
console.log(response)
app.append(response)
})
Run Code Online (Sandbox Code Playgroud)
Eri*_*ic 0
您需要使用 ejs 模板之类的东西。
例如,在 app.js 中您将拥有:
app.get('/', function(req, res) {
res.render('index', {base: req.protocol + "://" + req.headers.host});
});
Run Code Online (Sandbox Code Playgroud)
然后在index.ejs中你将有
<!DOCTYPE html>
<html lang="en">
<head>
<title>Websitename</title>
<base href="<%- base %>">
</head>
Run Code Online (Sandbox Code Playgroud)
确保将视图引擎设置为靠近 app.js 顶部
app.set('view engine', 'ejs');
Run Code Online (Sandbox Code Playgroud)