[英]Express render external Json to jade
我有一个文件(api.js),当我使用node.js在终端中调用它时,它会提供有效的JSON响应。 我已经使用request-promise来执行http请求,并且该应用程序是Express的样板。
现在,我想将该响应添加到Jade文件中,并让Jade迭代JSON结果。
我如何获得快递使用该文件并将其传递给玉的信息?
其次但不是必须的,我如何在Jade中获得一个按钮以使用相同的api发出POST请求,或者前端如何调用后端并在前端显示结果?
这是我的api文件api.js:
var rp = require('request-promise');
var initGet = {
uri: 'http://www.jsonapi.com/get',
method: 'GET',
headers: {"Content-Type": "application/json"}
};
var initPost = {
uri: 'http://www.jsonapi.com/post',
method: 'POST',
headers: {"Content-Type": "application/json"},
data: {},
resolveWithFullResponse: true
};
var apiCall = function apiCall(options) {
// if request is GET
if (options.method === 'GET') {
rp(options)
.then(function (res) {
/// I assume this is where the response is sent to jade
})
.catch(console.error);
}
// if request is POST
else {
rp(options)
.then(function (res) {
/// I assume this is where the response is sent to jade
})
.catch(console.error);
}
};
var apiGet = function apiGet() {
apiCall(initGet);
};
var apiPost = function apiPost(input) {
initPost.data = input;
apiCall(initPost);
};
// example of data in POST
apiPost({
user: 2,
event: 'World Cup 2018',
name: 'Brazil'
});
module.exports = {
apiGet: apiGet,
apiPost: apiPost
};
在玉文件中,我有:
extends layout
block content
.title
h1
| App
.ui
each val in res
.ui_box
.ui_box__inner
.event
span val.event
.name
span val.name
.drop
p show drop down
.arrow
.ui_box.dropdown
.submit-button
p submit
//submit POST
这是经过反复试验的我的解决方案!!!
我继续并使用了对外部jSON api的http调用请求 。
api.js:
var request = require('request'); // require in request
var initGet = {uri: 'http://linkToApi.com/get'};
var initPost = {uri: 'http://http://linkToApi.com/post'};
var apiCaller = function (url, cb) {
//use request to make the external http call to the JSON api
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
cb(body);// Send body/response to callback
}
})
};
// Call the api with a call back
var apiGet = function(cb) {
return apiCaller(initGet.uri, cb);
};
var apiPost = function(post, cb) {
return apiCaller(initGet.uri + post, cb);
};
// Export the functions for external access
module.exports = {
apiGet: apiGet,
apiPost: apiPost
};
现在为快递路线:
var api = require('./api');
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
//call the api apiGet and create callback function
api.apiGet(function (data) {
// render to the index.jade and pass the data from api call
res.render('index', { result :data});
});
});
最后在index.jade文件中:
block content
.ui
//*** make sure the indentation is correct 'for' otherwise it doesn't parse!!
for data in result //iterate through the results
.ui_box
.ui_box__inner
.event
span #{data.event} // here pick out the jSON you require
.name
span #{data.name}
我不确定我是否完全理解您的问题,但我不会100%确定。
就像您所说的那样,您不会“得到明确的使用权,然后将其传递给jade”,而只是在请求到服务器的情况下呈现带有某些数据的jade文件。 如果需要,该请求可以使用您的模块,但是用这种方式表述有助于其背后的概念。
有关如何在Express中使用模板引擎的信息,请阅读以下内容: http : //expressjs.com/guide/using-template-engines.html
您的端点将看起来像这样:
var yourModule = require('./modules/yourModuleFile'); //note you don't need .js
app.get('/', function (req, res) {
yourModule.apiGet().then(function(result){
res.render('yourTemplate', result);
})
})
在写完该示例之后,我认为您可能对如何实际使用Promise略有不同的想法。 您无需在模块内部“完成工作”,而是“返回以结果解决的承诺”。
如果您需要在这最后一点上做更多说明,请告诉我,我将扩大答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.