如何在快速渲染视图中调用节点模块?

时间:2017-09-14 02:22:25

标签: node.js express model-view-controller handlebars.js

我很难理解如何将调用数据传递给我的视图。我在一个小应用程序中需要Mozscape节点模块我正在尝试开发但我不太明白我将如何将信息传递到我的视图中。我想我需要在路由器中创建对象并在我的视图中调用该函数。 Mozscapes返回一个对象,我会将其传递到我的视图中,然后使用 Handlebars 来迭代数据?

以下是我的路由器下面的代码

//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未定义。我想我需要将我的业务对象移动到它自己的变量(后来的quired响应)中,然后将该函数放入我的路由器并从那里访问商业网站?

预期产出:

Object {pda: 24.123844872381643, uid: 187, upa: 33.43142060578742}

1 个答案:

答案 0 :(得分:0)

function storeData(param) {
    businessSeo = param;
}

//middleware not needed to process data for view

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); sent data to the variable
        storeData(res);
    });






//use declare router for http methods
//get request for index with request, response, next params
index.get('/', function (req, res, next) {
    //data has arrived!
    console.log(businessSeo);