admin管理员组文章数量:1321435
How does one display text held in a variable on the server side node.js in html.
In other words, just to be doubly clear, I have output pulled from an api that accesses twitter (Twitt) and I am trying to display that text at the bottom of the page.
This is how the webpage is called
app.get('/', function (req, res) {
res.sendfile('./Homepage.html');
});
This is where the Node.js files are called:
var express = require('express');
var app = express();
And here is where the variable is:
T.get('search/tweets', { q: hash1 + ' since:2013-11-11', count: 5 },
function(err, reply) {
response = (reply),
console.log(response)
app.append(response)
})
How does one display text held in a variable on the server side node.js in html.
In other words, just to be doubly clear, I have output pulled from an api that accesses twitter (Twitt) and I am trying to display that text at the bottom of the page.
This is how the webpage is called
app.get('/', function (req, res) {
res.sendfile('./Homepage.html');
});
This is where the Node.js files are called:
var express = require('express');
var app = express();
And here is where the variable is:
T.get('search/tweets', { q: hash1 + ' since:2013-11-11', count: 5 },
function(err, reply) {
response = (reply),
console.log(response)
app.append(response)
})
Share
Improve this question
edited Dec 5, 2013 at 23:00
Sumner Evans
9,1655 gold badges31 silver badges48 bronze badges
asked Dec 5, 2013 at 22:46
user2954467user2954467
951 gold badge1 silver badge4 bronze badges
1
-
1
I don't understand exactly where your call to
T
is. You will need to call that from within your express route to be able to send its output with your response. – Nick Tomlin Commented Dec 5, 2013 at 22:54
2 Answers
Reset to default 3Assuming the code to inject on the page will change on every request, you should have the code more or less as follows:
app.get('/', function (req, res){
// make the call to twitter before sending the response
var options = { q: hash1 + ' since:2013-11-11', count: 5 };
T.get('search/tweets', options, function(err, reply) {
// use RENDER instead of SENDFILE
res.render('./Homepage.html', {data: reply});
});
});
Notice how (1) I am making the call to Twitter inside the request, (2) I am using res.render() (http://expressjs./api.html#res.render) rather than res.sendFile().
This way, your view (homepage.html) can inject the data passed to res.render(). Depending on the template engine that you are using the syntax might be different, but if you are using EJS the following should work:
<p>your html goes here</p>
<p>The data from Twitter was <h2><%= data %></h2></p>
<p>xxx</p>
You need to use something like ejs templating.
For example, in app.js you will have:
app.get('/', function(req, res) {
res.render('index', {base: req.protocol + "://" + req.headers.host});
});
And then inside index.ejs you will have
<!DOCTYPE html>
<html lang="en">
<head>
<title>Websitename</title>
<base href="<%- base %>">
</head>
Make sure to set your view engine like so near the top of app.js
app.set('view engine', 'ejs');
本文标签: javascriptHow to print to html with express nodejsStack Overflow
版权声明:本文标题:javascript - How to print to html with express node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742102805a2420880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论