快速渲染外部Json到玉器

我有一个文件(api.js),当我使用node.js调用terminal时,它提供了一个工作的JSON响应。 我已经使用请求承诺来执行http请求,应用程序是Express模板。

现在我想将这个响应添加到Jade文件中,并让Jade迭代JSON结果。

我如何得到使用这个文件,然后传递给玉?

其次,但不是必须的,我怎么会得到一个Jadebutton使用相同的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 

这是我的解决scheme经过多次试验和错误!

我继续前进,并使用请求我的http调用外部jSON API。

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%肯定的,但我会放弃它。

你不会“放弃使用这个文件,然后把它传递给玉”,你只需要在服务器上发送一个带有一些数据的玉文件。 如果你愿意的话,这个请求可以使用你的模块,但是用这种方式来说明它的背后的概念是有帮助的。

有关如何使用快速模板引擎的信息阅读此: 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); }) }) 

写完这个例子之后,我想你可能会对如何实际使用承诺有一些不同的想法。 你不要在你的模块中“做工作”,而是“回到解决结果的承诺”。

如果你需要更多的解释,最后一点让我知道,我会扩大我的答案。