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

Node.js 为我的节点项目开发正确的结构和设计模式,node.js,api,design-patterns,model-view-controller,Node.js,Api,Design Patterns,Model View Controller,导言 第一次使用Node并感谢所有帮助和改进 我已经开发了代码 第一个从帖子中获取数据 其次,验证API连接并获取密钥 使用第一个和第二个函数中的变量并将其发布到API 问题 这段代码正在运行,并且是后端的。然而,我并没有把MVC放在心上构建它,我希望有人能帮助我清理它,并更好地构建它 我是node的新手,希望我的第一个项目遵循整洁的MVC结构,我可以将其用作如何构建未来产品的想法 我拥有的 正如前面所说,代码可以工作,但是它都在根目录上的index.js中,因为它是我的应用程序的后端,我希望它

导言

第一次使用Node并感谢所有帮助和改进

我已经开发了代码

  • 第一个从帖子中获取数据
  • 其次,验证API连接并获取密钥
  • 使用第一个和第二个函数中的变量并将其发布到API
  • 问题

    这段代码正在运行,并且是后端的。然而,我并没有把MVC放在心上构建它,我希望有人能帮助我清理它,并更好地构建它

    我是node的新手,希望我的第一个项目遵循整洁的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);
    

    要首先谈论设计模式,您必须将任何对象/服务提取为对象。而不是试图找到提取对象之间的关系-没有关系?没有设计模式,抱歉……不用担心,我们仍在努力学习构造代码和构建未来实现的最佳方法