您将如何在单个页面应用上显示已解析的数据。例如,我能够在console.log中生成从API生成的引号,但我不确定如何在单页上发布它们。
Index.ejs:
<% include ./partials/header %>
<div class=container>
<h1>Free2BGeek</h1>
<h3>This is your joke for the day!</h3>
<hr>
<p>
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sit amet turpis ex. Vivamus aliquet ipsum tortor, at rhoncus ligula efficitur condimentum. Sed pretium arcu quis ex fringilla, et consectetur odio sodales."
</p>
</div>
<% include ./partials/footer %>
app.js
const express = require("express"),
app = express(),
request = require("request"),
bodyParser = require("body-parser"),
mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/geek-jokes", { useNewUrlParser: true });
mongoose.set('useFindAndModify', false);
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.get("/", function(req, res){
res.render("index");
});
let options = {
url: 'https://geek-jokes.sameerkumar.website/api',
method: 'GET'
};
request(options, (err, response, body) => {
if(!err && response.statusCode == 200)
var parsedData = JSON.parse(body);
console.log(parsedData);
});
app.get("*", function(req, res){
res.send("Error - I'm sorry, the page you tried to get to either does not exist or has been removed.");
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Geeks are ready!");
});
我希望能够用笑话取代Lorem Ipsum文本。
我目前的结果如下:
我试着把
<p>
<%= options %>
</p>
但我只得到一个错误。我应该使用POST路由替换ext吗?或者是否有另一种方法来嵌入来自api的引用?
===========================================================================
感谢THEWOLF和Lawrence Cherone。代码在app.js文件中更新,如下所示:
const express = require("express"),
app = express(),
request = require("request"),
bodyParser = require("body-parser"),
mongoose = require("mongoose");
var url = process.env.DATABASEURL || "mongodb://localhost:27017/geek-jokes";
mongoose.connect(url);
mongoose.set('useFindAndModify', false);
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.get("/", function(req, res){
let options = {
url: 'https://geek-jokes.sameerkumar.website/api',
method: 'GET'
};
request(options, (err, response, body) => {
if(!err && response.statusCode == 200)
var parsedData = JSON.parse(body);
console.log(parsedData);
res.render("index", {body: parsedData });
});
});
app.get("*", function(req, res){
res.send("Error - I'm sorry, the page you tried to get to either does not exist or has been removed.");
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Geeks are ready!");
});
你必须像这样传递你想要发送到前端的数据对象
app.get("/", function(req, res){
res.render("index",{data:'your data like this in here'}); // change like this and acess it in front end the way you have done it
});
let options = {
url: 'https://geek-jokes.sameerkumar.website/api',
method: 'GET'
};
request(options, (err, response, body) => {
if(!err && response.statusCode == 200)
var parsedData = JSON.parse(body);
console.log(parsedData);
});