0
我在试图了解如何将所谓的数据传递给我的视图时遇到了很困难的时间。我正在试图开发一个小应用程序需要Mozscape节点模块,但我不太明白如何将这些信息传递到我的视图中。我会想象我需要在我的路由器中创建对象并在我的视图中调用该函数。 Mozscapes返回一个对象,我将它传递到我的视图中,然后使用把手遍历数据?如何在快速渲染视图中调用节点模块?
下面是从我的路由器下面
//pull in express
const express = require('express');
//declare router as variable for index view
const index = express.Router();
//mozscape for seo data
var Mozscape = require('mozscape').Mozscape;
//mozscape expects accessid and secretid
//only allows for 1 call per 10 seconds with free plan
//need to add to env file later
var moz = new Mozscape('mozscape-6d6ab44235', '846aca62a6db69661c81b784115e8a9');
//dummy data
var business =
{
name: 'Party Corner',
address: '123 Main Street, Red Bank, NJ',
hours: 'Monday through Friday 9 - 5',
website: 'http://www.partycorner.com/',
category: 'Party Supplies',
imgUrl: 'https://scontent.fewr1-3.fna.fbcdn.net/v/t31.0-8/14361226_10154547117788288_58127422291546970_o.jpg?oh=126b8240a8ac27dfb06b57ed51f4320e&oe=5A5A8FCC'
};
//middleware to process data for view
var businessSeo;
businessSeo = function (req, res, next) {
req.businessSeo = moz.urlMetrics(business.website, ['page_authority', 'links', 'domain_authority'], function (err, res) {
if (err) {
console.log(err);
return;
}
console.log('requesting data from moz');
console.log(res);
return res;
});
};
index.use(businessSeo);
//logging the data
console.log(businessSeo);
//use declare router for http methods
//get request for index with request, response, next params
index.get('/', function (req, res, next) {
//render response from server using index view from declared path in app.js
res.render('index', {
//declare {{ title }} used in main template extended as default template
title: "Business Directory",
//use business as object keys and values.. |key val|
business: business,
body:
{
description: 'This is a business for the business directory, how neat is that?'
},
mozData: businessSeo
})
});
module.exports = index;
我只是想现在登录前端的对象的代码,返回该MOZ没有定义。我想我需要将我的业务对象转移到它自己的变量中(以后需要响应),然后将该功能放入我的路由器并从那里访问业务网站?
预期输出:
Object {pda: 24.123844872381643, uid: 187, upa: 33.43142060578742}
几乎可以肯定你需要调用'下一个()',而不是'返回res',因为这将在此功能停止中间件,甚至打你的主前返回**/**路径的响应函数。改变它next(),你可能会很好。 – Andrei
嘿安德烈这就是我的想法,但事实证明,它根本不需要中间件。我回答了我自己的问题,在一分钟后在这里发帖。需要找到一种让它更优雅的方式 –