为我的节点项目开发正确的结构和设计模式

时间:2017-06-13 14:35:20

标签: node.js api design-patterns model-view-controller

简介

  

首次使用Node并感谢所有帮助和改进。

我开发了代码

  1. 首先从帖子中获取数据
  2. 其次验证API连接并获取密钥
  3. 使用第一个和第二个函数中的变量并发布到API
  4. 问题

    此代码全部正常运行,并且是后端节点。但是我没有用MVC构建它,我希望有人帮我清理它并更好地构建它。

    我是节点的新手,希望我的第一个项目能够遵循一个简洁的MVC结构,我可以将其用作构建未来产品的想法。

    我有什么

    如前所述,代码可以正常工作,但它位于根目录的index.js之内,因为它是我的应用程序的后端,我希望它位于名为backend的文件夹中然后是文件夹或者来自那里的文件。

    index.js

    //---------------------------------- Grab the packages we need and set variables ---------------------------------------
    //----------------------------------------------------------------------------------------------------------------------
    var express = require('express');
    var request = require('request');
    var nodePardot = require('node-pardot');
    var bodyParser = require('body-parser');
    var app = express();
    var port = process.env.PORT || 8080;
    
    // Credential's for pardot API
    var password = '######';
    var userkey = '######';
    var emailAdmin = '######';
    
    // Start the server
    app.listen(port);
    app.use(bodyParser.json()); // support json encoded bodies
    app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
    console.log('Server started! At http://localhost:' + port); // Confirms server start
    
    //---------------------------------- Function to get front end form posted data ----------------------------------------
    //----------------------------------------------------------------------------------------------------------------------
    var firstFunction = function () {
        var promise = new Promise(function (resolve) { // may be redundant
            setTimeout(function () {
                app.post('/api/data', function (req, res) {
                    console.log(req.body);
                    // Get varibles from the post form
                    var email_user = req.body.email;
                    var first_name = req.body.fname;
                    var last_name = req.body.lname;
    
                    // res.send(email_user);
                    res.send(first_name + ' ' + last_name + ' ' + email_user);
    
                    //resolve when get the response
                    resolve({
                        data_email: email_user,
                        data_first_name: first_name,
                        data_last_name: last_name
                    });
                });
    
            }, 2000);
        });
        return promise;
    };
    
    //---------------------------------- Function to get API key from Pardot (AUTHENTICATION) ------------------------------
    //----------------------------------------------------------------------------------------------------------------------
    var secondFunction = function () {
        var promise = new Promise(function (resolve) {
            setTimeout(function () {
                nodePardot.PardotAPI({
                    userKey: userkey,
                    email: emailAdmin,
                    password: password,
                    DEBUG: false
                }, function (err, client) {
                    if (err) {
                        // Authentication failed
                        console.error("Authentication Failed", err);
                    } else {
                        // Authentication successful
                        var api_key = client.apiKey;
                        console.log("Authentication successful !", api_key);
                        resolve({data_api: api_key});
                    }
                });
    
            }, 2000);
        });
        return promise;
    };
    
    //---------------------------------- Function to post data to Pardot ---------------------------------------------------
    // ---------------------------------------------------------------------------------------------------------------------
    function thirdFunction(result) {
        var promise = new Promise(function () {
            setTimeout(function () {
                var headers = {
                    'User-Agent': 'Super Agent/0.0.1',
                    'Content-Type': 'application/x-www-form-urlencoded'
                };
    
    // Configure the request
                var useremail = result[0].data_email;
                var api = result[1].data_api;
                var Fname = result[0].data_first_name;
                var Lname = result[0].data_last_name;
                var options = {
                    url: 'https://pi.pardot.com/api/prospect/version/4/do/create/email',
                    method: 'POST',
                    headers: headers,
                    form: {
                        'email': useremail,
                        'user_key': userkey,
                        'api_key': api,
                        'first_name': Fname,
                        'last_name': Lname
                    }
                };
    
    // Start the request
                request(options, function (error, response) {
                    if (!error && response.statusCode == 200) {
                        console.log("error", error);
                    }
                    else {
                        console.log("Sent Data successful !");
                        console.log("API Key", api);
                        console.log("user", useremail);
                        console.log("userKey", userkey);
                        console.log("name", Fname);
                        console.log("surname", Lname);
                    }
                });
            }, 3000);
        });
        return promise;
    }
    
    // sequence of functions
    Promise.all([firstFunction(), secondFunction()])
        .then(thirdFunction);
    

0 个答案:

没有答案