简体   繁体   English

模块和require()

[英]Modules and require()

I got two files one between1.js and the second one is import.js. 我得到了两个文件,其中一个介于1.js之间,第二个是import.js。 In between1.js I got few functions and one of the functions I moved to import.js. 在between1.js中,我只有几个功能,而其中一个功能已移至import.js。 The exercise I got is to use the module and require function. 我得到的练习是使用模块并需要功能。 The problem I still have how to use require in between1.js. 我仍然有如何在between1.js中使用require的问题。 See code below 见下面的代码

Between1.js code : https://codeshare.io/alxMvY Between1.js代码: https : //codeshare.io/alxMvY

import.js : https://codeshare.io/244yg4 import.js: https ://codeshare.io/244yg4

I am beginners on js and node. 我是js和node的初学者。 I got function in import.js and on the bottom of code is module.exports that mean exporting module and in the second file between1.js, I try to import the function by first : let utils = require("./import.js"); 我在import.js中获得函数,并且在代码底部是module.exports,这意味着要导出模块,并且在1.js之间的第二个文件中,我尝试首先导入该函数:let utils = require(“ ./ import.js “); And on the bottom trying to get the function but the problem is I don't know really how. 在底部尝试获取功能,但是问题是我真的不知道如何。 I was trying like I paste before in code but it dosesn't work 我试图像以前在代码中粘贴一样,但是没有用

What I want is to someone help me explain how to require and module work. 我想要的是有人帮助我解释如何要求和模块工作。

import.js code : import.js代码:

    /**
 * Module.
 *
 * Matteus Urbaniak
 */
 "use strict";


function teacherAsTable(res) {
     let str;

     str = "+-----------+---------------------+-----------+----------+--------------+\n";
     str += "| Akronym   | Namn                | Avdelning |   Lön    | Kompetens    |\n";
     str += "|-----------|---------------------|-----------|----------|--------------|\n";
     for (const row of res) {
         str += "| ";
         str += row.akronym.padEnd(10);
         str += "| ";
         str += (row.fornamn + " " + row.efternamn).padEnd(20);
         str += "| ";
         str += row.avdelning.padEnd(10);
         str += "| ";
         str += row.lon.toString().padStart(9);
         str += "| ";
         str += row.kompetens.toString().padEnd(12);
         str += " |\n";
     }
     str += "+-----------+---------------------+-----------+----------+--------------|\n";

     return str;
 }

module.exports = {
    "teacherAsTable": teacherAsTable
};

between1.js between1.js

    /**
Arbete gjordes av Matteus Urbaniak
*/


"use strict";


/**
Jag använder programmeringsstilen med async/await
för att hantera asynkrona anrop. För det syftet väljer
jag paketet promise-mysql som är en wrapper kring paketet mysql.
*/

const mysql = require("promise-mysql");

// Inloggningsdetaljer till databasen.
const config = require("./config.json");

let utils = require("./import.js");

// Läsa från komand linje

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

/**
Main function
*/

(async function() {
    const db = await mysql.createConnection(config);
    let str;

    rl.question("What do you want search for? (min); ", async (min) => {
        rl.question("What do you want search for? (max); ", async (max) => {
            str = await searchBetween(db, min, max);
            console.info(str);
            rl.close();
            db.end();
        });
    });
})();



// Ge en rapport med larare details och formaterad text.

async function searchBetween(db, min, max) {
    let sql;
    let res;
    let str;
    let minimum = `${min}`;
    let maximum = `${max}`;

    console.info(`Searching for: ${minimum} - ${maximum}`);
    sql = `
        SELECT
            akronym,
            fornamn,
            efternamn,
            avdelning,
            lon,
            kompetens
        FROM larare
        WHERE
            (kompetens BETWEEN ? AND ?)
            OR
            lon BETWEEN ? AND ?
        ORDER BY akronym;
    `;
    res = await db.query(sql, [minimum, maximum, minimum, maximum]);
    str = teacherAsTable(res);
    return str;
}

// HERE SHOULD BE teacherAsTable 
let res;

res = utils.teacherAsTable();
console.log(res);

尝试在Between1.js中执行以下操作:

module.exports = {teacherAsTable};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM