精华内容
下载资源
问答
  • 内部要求 导入一个模块,并公开其内部结构。 主要用于测试通常隐藏的功能,但您也可以使用它进行其他类型的黑客攻击。 注意:内部似乎只能在 v0.10.x 上读取。 我不知道为什么; 如果你知道,告诉我!...
  • node 导出csv文件A great library you can use to quickly write an array of objects to a CSV file using Node.js is objects-to-csv. 可以使用Node.js将objects-to-csv数组快速写入CSV文件的出色库是objects-to...

    node 导出csv文件

    A great library you can use to quickly write an array of objects to a CSV file using Node.js is objects-to-csv.

    可以使用Node.js将objects-to-csv数组快速写入CSV文件的出色库是objects-to-csv

    Many other libraries exist, of course. I found this useful for a project of mine where I had to generate a one-time CSV file, so I wrote this little tutorial.

    当然,还有许多其他库。 我发现这对我的一个项目很有用,因为我必须生成一个一次性CSV文件,因此我写了这个小教程。

    Using a stream-based library like fast-csv might suits your needs in more performance-oriented applications.

    使用像fast-csv这样的基于的库可能适合您更注重性能的应用程序中的需求。

    Install it using:

    使用以下方法安装:

    npm install objects-to-csv

    then require it in your Node.js code:

    然后在您的Node.js代码中要求它:

    const ObjectsToCsv = require('objects-to-csv')

    When you have an array of objects ready to write to CSV, initialize a new ObjectsToCsv object instance:

    当您准备好要写入CSV的对象数组时,请初始化一个新的ObjectsToCsv对象实例:

    const csv = new ObjectsToCsv(list)

    then call csv.toDisk(), passing the file you want to write to (relative to your app base path):

    然后调用csv.toDisk() ,传递您要写入的文件(相对于您的应用程序基本路径):

    await csv.toDisk('./list.csv')

    This is a promise-based API and I used await, so you need to call this inside an async function.

    这是一个基于promise的API,我使用了await ,因此您需要在一个异步函数中调用它。

    The column names in the CSV are automatically inferred from the object properties names.

    CSV中的列名称是根据对象属性名称自动推断出来的。

    Note that this command overwrites the existing content of the file. To append to that file, pass a second object with the append property set to true:

    请注意,此命令将覆盖文件的现有内容。 要附加到该文件,请传递第二个对象,将append属性设置为true:

    await csv.toDisk('./list.csv', { append: true })

    翻译自: https://flaviocopes.com/node-write-csv/

    node 导出csv文件

    展开全文
  • linux动态链接库导出函数控制

    千次阅读 2017-03-05 15:33:35
    linux动态链接库导出函数控制 windows 环境的vc的话,可以方便的指定__declspec(dllexport) 关键字来控制是否把dll中的函数导出。 我也来测试一下linux下面是如何做的: 先看gcc 和ld的相关选项 =============...

    linux动态链接库导出函数控制

    windows 环境的vc的话,可以方便的指定__declspec(dllexport) 关键字来控制是否把dll中的函数导出。
    我也来测试一下linux下面是如何做的:
    先看gcc 和ld的相关选项

    ======================================
    gcc 选项
           -shared
               Produce a shared object which can then be linked with other objects
               to form an executable. Not all systems support this option. For
               predictable results, you must also specify the same set of options
               that were used to generate code (-fpic, -fPIC, or model suboptions)
               when you specify this option.[1]


           -fpic
               Generate position-independent code (PIC) suitable for use in a
               shared library, if supported for the target machine. Such code
               accesses all constant addresses through a global offset table
               (GOT). The dynamic loader resolves the GOT entries when the
               program starts (the dynamic loader is not part of GCC; it is part
               of the operating system). If the GOT size for the linked
               executable exceeds a machine-specific maximum size, you get an
               error message from the linker indicating that -fpic does not work;
               in that case, recompile with -fPIC instead. (These maximums are 8k
               on the SPARC and 32k on the m68k and RS/6000. The 386 has no such
               limit.)

               Position-independent code requires special support, and therefore
               works only on certain machines. For the 386, GCC supports PIC for
               System V but not for the Sun 386i. Code generated for the IBM
               RS/6000 is always position-independent.

               When this flag is set, the macros "__pic__" and "__PIC__" are
               defined to 1.

           -fPIC
               If supported for the target machine, emit position-independent
               code, suitable for dynamic linking and avoiding any limit on the
               size of the global offset table. This option makes a difference on
               the m68k, PowerPC and SPARC.

               Position-independent code requires special support, and therefore
               works only on certain machines.

               When this flag is set, the macros "__pic__" and "__PIC__" are
               defined to 2.

           -rdynamic
               Pass the flag -export-dynamic to the ELF linker, on targets that
               support it. This instructs the linker to add all symbols, not only
               used ones, to the dynamic symbol table. This option is needed for
               some uses of "dlopen" or to allow obtaining backtraces from within
               a program.


          -fvisibility=default|internal|hidden|protected
               Set the default ELF image symbol visibility to the specified
               option---all symbols will be marked with this unless overridden
               within the code. Using this feature can very substantially improve
               linking and load times of shared object libraries, produce more
               optimized code, provide near-perfect API export and prevent symbol
               clashes. It is strongly recommended that you use this in any
               shared objects you distribute.

               Despite the nomenclature, "default" always means public ie;
               available to be linked against from outside the shared object.
               "protected" and "internal" are pretty useless in real-world usage
               so the only other commonly used option will be "hidden". The
               default if -fvisibility isn't specified is "default", i.e., make
               every symbol public---this causes the same behavior as previous
               versions of GCC.

               A good explanation of the benefits offered by ensuring ELF symbols
               have the correct visibility is given by "How To Write Shared
               Libraries" by Ulrich Drepper (which can be found at
               <http://people.redhat.com/~drepper/>)---however a superior solution
               made possible by this option to marking things hidden when the
               default is public is to make the default hidden and mark things
               public. This is the norm with DLL's on Windows and with
               -fvisibility=hidden and "__attribute__ ((visibility("default")))"
               instead of "__declspec(dllexport)" you get almost identical
               semantics with identical syntax. This is a great boon to those
               working with cross-platform projects.


               For those adding visibility support to existing code, you may find
               #pragma GCC visibility of use. This works by you enclosing the
               declarations you wish to set visibility for with (for example)
               #pragma GCC visibility push(hidden) and #pragma GCC visibility pop.
               Bear in mind that symbol visibility should be viewed as part of the
               API interface contract and thus all new code should always specify
               visibility when it is not the default ie; declarations only for use
               within the local DSO should always be marked explicitly as hidden
               as so to avoid PLT indirection overheads---making this abundantly
               clear also aids readability and self-documentation of the code.
               Note that due to ISO C++ specification requirements, operator new
               and operator delete must always be of default visibility.

               Be aware that headers from outside your project, in particular
               system headers and headers from any other library you use, may not
               be expecting to be compiled with visibility other than the default.
               You may need to explicitly say #pragma GCC visibility push(default)
               before including any such headers.

               extern declarations are not affected by -fvisibility, so a lot of
               code can be recompiled with -fvisibility=hidden with no
               modifications. However, this means that calls to extern functions
               with no explicit visibility will use the PLT, so it is more
               effective to use __attribute ((visibility)) and/or #pragma GCC
               visibility to tell the compiler which extern declarations should be
               treated as hidden.

               Note that -fvisibility does affect C++ vague linkage entities. This
               means that, for instance, an exception class that will be thrown
               between DSOs must be explicitly marked with default visibility so
               that the type_info nodes will be unified between the DSOs.

               An overview of these techniques, their benefits and how to use them
               is at <http://gcc.gnu.org/wiki/Visibility>.

    ===================================
    ld命令选项


           -E
           --export-dynamic
           --no-export-dynamic
               When creating a dynamically linked executable, using the -E option
               or the --export-dynamic option causes the linker to add all symbols
               to the dynamic symbol table. The dynamic symbol table is the set
               of symbols which are visible from dynamic objects at run time.

               If you do not use either of these options (or use the
               --no-export-dynamic option to restore the default behavior), the
               dynamic symbol table will normally contain only those symbols which
               are referenced by some dynamic object mentioned in the link.

               If you use "dlopen" to load a dynamic object which needs to refer
               back to the symbols defined by the program, rather than some other
               dynamic object, then you will probably need to use this option when
               linking the program itself.

               You can also use the dynamic list to control what symbols should be
               added to the dynamic symbol table if the output format supports it.
               See the description of --dynamic-list.

               Note that this option is specific to ELF targeted ports. PE
               targets support a similar function to export all symbols from a DLL
               or EXE; see the description of --export-all-symbols below.


           --dynamic-list=dynamic-list-file
               Specify the name of a dynamic list file to the linker. This is
               typically used when creating shared libraries to specify a list of
               global symbols whose references shouldn't be bound to the
               definition within the shared library, or creating dynamically
               linked executables to specify a list of symbols which should be
               added to the symbol table in the executable. This option is only
               meaningful on ELF platforms which support shared libraries.

               The format of the dynamic list is the same as the version node
               without scope and node name. See VERSION for more information.

           --dynamic-list-data
               Include all global data symbols to the dynamic list.


           --version-script=version-scriptfile
               Specify the name of a version script to the linker. This is typically used when creating shared libraries to
               specify additional information about the version hierarchy for the library being created. This option is only
               fully supported on ELF platforms which support shared libraries; see VERSION. It is partially supported on PE
               platforms, which can use version scripts to filter symbol visibility in auto-export mode: any symbols marked
               local in the version script will not be exported.

         #VERSION 文件格式可以参考这里     http://sourceware.org/binutils/docs-2.20/ld/VERSION.html#VERSION
    =========================================


    测试看看:


    -----------------------main.c -----------------------------------
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>

    #include<dlfcn.h>

    int test (int i)
    {

    printf("i=%d\n" ,i);
    }

    int main() 
    {
        int (*test2)(int);
        int (*test3)(int);
        int *handler;

     

        handler=(int*)dlopen("test.so", RTLD_LAZY);
        if (!handler) {
            printf( "加载模块错误 %s\n", dlerror() );
            return;
        }

        test3 = dlsym(handler, "test3");
            if (test3) test3(3);

         test2 = dlsym(handler, "test2");
            if (test2) test2(2);

            dlclose(handler);

    exit(0);

    }

    ------------------------------------------------------------------
    $ gcc -rdynamic main.c -o main -ldl

    $ readelf -s main

    Symbol table '.dynsym' contains 25 entries:
       Num:    Value Size Type    Bind   Vis      Ndx Name
         0: 00000000     0 NOTYPE LOCAL DEFAULT UND 
         1: 00000000     0 NOTYPE WEAK   DEFAULT UND __gmon_start__
         2: 00000000     0 NOTYPE WEAK   DEFAULT UND _Jv_RegisterClasses
         3: 00000000     0 FUNC    GLOBAL DEFAULT UND (2)
         4: 00000000     0 FUNC    GLOBAL DEFAULT UND (3)
         5: 00000000     0 FUNC    GLOBAL DEFAULT UND (2)
         6: 00000000     0 FUNC    GLOBAL DEFAULT UND (4)
         7: 00000000     0 FUNC    GLOBAL DEFAULT UND (3)
         8: 00000000     0 FUNC    GLOBAL DEFAULT UND (2)
         9: 00000000     0 FUNC    GLOBAL DEFAULT UND (3)
        10: 0804a020     0 NOTYPE GLOBAL DEFAULT   24 __data_start
        11: 0804a028    22 OBJECT GLOBAL DEFAULT   24 str
        12: 0804a048     0 NOTYPE GLOBAL DEFAULT ABS _end
        13: 0804a040     0 NOTYPE GLOBAL DEFAULT ABS _edata
        14: 0804a020     0 NOTYPE WEAK   DEFAULT   24 data_start
        15: 080486b0     0 FUNC    GLOBAL DEFAULT   14 _start
        16: 080488f8     4 OBJECT GLOBAL DEFAULT   16 _fp_hw
        17: 080488fc     4 OBJECT GLOBAL DEFAULT   16 _IO_stdin_used
        18: 08048850    90 FUNC    GLOBAL DEFAULT   14 __libc_csu_init
        19: 0804a040     0 NOTYPE GLOBAL DEFAULT ABS __bss_start
        20: 08048764    28 FUNC    GLOBAL DEFAULT   14 test   ///test 在这里导出了
        21: 08048780   178 FUNC    GLOBAL DEFAULT   14 main
        22: 080485ec     0 FUNC    GLOBAL DEFAULT   12 _init
        23: 08048840     5 FUNC    GLOBAL DEFAULT   14 __libc_csu_fini
        24: 080488dc     0 FUNC    GLOBAL DEFAULT   15 _fini


    ----------------------------------------------------------------------------------

    $ gcc main.c -o main -ldl

    $ readelf -s main

    Symbol table '.dynsym' contains 11 entries:
       Num:    Value Size Type    Bind   Vis      Ndx Name
         0: 00000000     0 NOTYPE LOCAL DEFAULT UND 
         1: 00000000     0 NOTYPE WEAK   DEFAULT UND __gmon_start__
         2: 00000000     0 NOTYPE WEAK   DEFAULT UND _Jv_RegisterClasses
         3: 00000000     0 FUNC    GLOBAL DEFAULT UND (2)
         4: 00000000     0 FUNC    GLOBAL DEFAULT UND (3)
         5: 00000000     0 FUNC    GLOBAL DEFAULT UND (2)
         6: 00000000     0 FUNC    GLOBAL DEFAULT UND (4)
         7: 00000000     0 FUNC    GLOBAL DEFAULT UND (3)
         8: 00000000     0 FUNC    GLOBAL DEFAULT UND (2) ///没有把 test函数导出
         9: 00000000     0 FUNC    GLOBAL DEFAULT UND (3)
        10: 080486fc     4 OBJECT GLOBAL DEFAULT   16 _IO_stdin_used


    ====================================================================
    $ gcc -rdynamic -fPIC main.c -o main -ldl

    $ readelf -s main

    可以看到有 '.dynsym' 节有把函数导出  

    =======================================================================

     

     

    ------test.c---------------------
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>


    extern int test (int i);

    int test2 (int i)
    {
         test(i);
         printf("this is test2\n");
    }

    int test3 (int i)
    {
         printf("this is test 3\n");
    }
    -----------------------------------------------

    $ gcc -shared -fPIC test.c -o test.so


    $ readelf -s test.so

    '.dynsym' 节里把所有函数导出

    ====================================================================

    $ ./main
    加载模块错误 test.so: cannot open shared object file: No such file or directory 

    找不到 test.so文件,他不会自动在当前目录下搜索啊。
    那就设置好这个LD_LIBRARY_PATH这个环境变量吧,我是不想复制到系统目录去了

    $ echo $LD_LIBRARY_PATH

    $ export LD_LIBRARY_PATH=`pwd`
    $ echo $LD_LIBRARY_PATH
    /home/widebright/桌面/测试用例

    --------------------------
    gcc -rdynamic -fPIC main.c -o main -ldl
    gcc -shared -fPIC test.c -o test.so
    使用上面编译选线,结果 
    $ ./main
    this is test 3
    i=2
    this is test2
    --------------------------------
    gcc -fPIC main.c -o main -ldl
    gcc -shared -fPIC test.c -o test.so
    使用上面编译选线,结果test.so就找不到 main中export出来的test函数了
    $ ./main 
    this is test 3
    ./main: symbol lookup error: /home/widebright/桌面/测试用例/test.so: undefined symbol: test
    --------------------------------
    gcc -rdynamic main.c -o main -ldl
    gcc -shared -fPIC test.c -o test.so
    使用上面编译选线,结果还是可以执行,说明 -fPIC对程序来说好像用处不大。
    $ ./main
    this is test 3
    i=2
    this is test2
    ------------------------------------------------
    gcc -rdynamic main.c -o main -ldl
    gcc -shared test.c -o test.so
    使用上面编译选线,也还能正确运行 -fPIC 有什么作用啊,不明显
    $ ./main
    this is test 3
    i=2
    this is test2
    ---------------------------------------------

     

    ----visibility.txt------------------

        global:
                test3;   
        local: *;
    };
    -------------------

    gcc -rdynamic main.c -o main -ldl
    gcc -shared test.c -o test.so -Wl,--version-script=visibility.txt

    $ gcc -shared test.c -o test.so -Wl,--version-script=visibility.txt
    $ ./main 
    this is test 3

    可以看到 使用--version-script=visibility.txt 参数控制之后,只有test3才被导出了

    :~/桌面/测试readelf -s test.so

    Symbol table '.dynsym' contains 7 entries:
       Num:    Value Size Type    Bind   Vis      Ndx Name
         0: 00000000     0 NOTYPE LOCAL DEFAULT UND 
         1: 00000000     0 NOTYPE WEAK   DEFAULT UND __gmon_start__
         2: 00000000     0 NOTYPE WEAK   DEFAULT UND _Jv_RegisterClasses
         3: 00000000     0 NOTYPE GLOBAL DEFAULT UND test
         4: 00000000     0 FUNC    GLOBAL DEFAULT UND (2)
         5: 00000000     0 FUNC    WEAK   DEFAULT UND (3)
         6: 0000041b    20 FUNC    GLOBAL DEFAULT   12 test3    //visibility.txt文件里面指定了 test3才导出了, test2不再导出了

    ======================================


    gcc -shared test.c -o test.so -fvisibility=hidden

    ----当 -fvisibility=hidden 设置为hidden之后,所有的都函数不再导出了。
    readelf -s test.so

    Symbol table '.dynsym' contains 11 entries:
       Num:    Value Size Type    Bind   Vis      Ndx Name
         0: 00000000     0 NOTYPE LOCAL DEFAULT UND 
         1: 00000000     0 NOTYPE WEAK   DEFAULT UND __gmon_start__
         2: 00000000     0 NOTYPE WEAK   DEFAULT UND _Jv_RegisterClasses
         3: 00000000     0 NOTYPE GLOBAL DEFAULT UND test    ///这个还是会在这里,知道是外部函数来的
         4: 00000000     0 FUNC    GLOBAL DEFAULT UND (2)
         5: 00000000     0 FUNC    WEAK   DEFAULT UND (3)
         6: 0000202c     0 NOTYPE GLOBAL DEFAULT ABS _end
         7: 00002024     0 NOTYPE GLOBAL DEFAULT ABS _edata
         8: 00002024     0 NOTYPE GLOBAL DEFAULT ABS __bss_start
         9: 0000035c     0 FUNC    GLOBAL DEFAULT   10 _init
        10: 000004e8     0 FUNC    GLOBAL DEFAULT   13 _fini

    ==============================================

    我们给要导出到函数加上 属性控制,改成这样,
    ----------test.c-----------------
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>


    extern int test (int i);

    __attribute ((visibility)) int test2 (int i)
    {
         test(i);
         printf("this is test2\n");
    }

    __attribute ((visibility)) int test3 (int i)
    {
         printf("this is test 3\n");
    }

    -------------------------------

    $ gcc -shared test.c -o test.so -fvisibility=hidden


    $ readelf -s test.so

    Symbol table '.dynsym' contains 13 entries:
       Num:    Value Size Type    Bind   Vis      Ndx Name
         0: 00000000     0 NOTYPE LOCAL DEFAULT UND 
         1: 00000000     0 NOTYPE WEAK   DEFAULT UND __gmon_start__
         2: 00000000     0 NOTYPE WEAK   DEFAULT UND _Jv_RegisterClasses
         3: 00000000     0 NOTYPE GLOBAL DEFAULT UND test
         4: 00000000     0 FUNC    GLOBAL DEFAULT UND (2)
         5: 00000000     0 FUNC    WEAK   DEFAULT UND (3)
         6: 0000202c     0 NOTYPE GLOBAL DEFAULT ABS _end
         7: 00002024     0 NOTYPE GLOBAL DEFAULT ABS _edata
         8: 000004bc    31 FUNC    GLOBAL DEFAULT   12 test2    //这次就会导出了
         9: 00002024     0 NOTYPE GLOBAL DEFAULT ABS __bss_start
        10: 000003a0     0 FUNC    GLOBAL DEFAULT   10 _init
        11: 00000528     0 FUNC    GLOBAL DEFAULT   13 _fini
        12: 000004db    20 FUNC    GLOBAL DEFAULT   12 test3


    $ ./main 
    this is test 3
    i=2
    this is test2
    -----------------------------------------------------

     


    总结:

    通过gcc命令的-fvisibility=hidden 选项和 "__attribute__ ((visibility("default")))" 语法扩展 可以得到 vc中
    __declspec(dllexport)"的效果。

    采用ld的 --version-script=version-scriptfile 参数 类似vc中到 *.DEF 文件,也可以用来统一链接库到输出与否。


    测完才发现,gcc帮助文档提到的 “How To Write Shared Libraries” 这篇文章上面解释的更完整一些,
    一个pdf文档,在google搜索一下就可以找到了。

    其他的办法还包括:
    1. static 定义函数

    2、libtool的-export-symbols参数
    $ libtool --mode=link gcc -o libfoo.la \
    foo.lo -export-symbols=foo.sym

    3.使用alias属性来隐藏具体的函数名
    int next (void) {    
    return ++last_int;         
    }                           
    extern __typeof (next) next_int
                                           
    __attribute ((alias ("next"),
                                             
                    visibility ("hidden")));

    展开全文
  • EXE导出函数及def文件格式说明

    千次阅读 2012-04-28 17:40:49
    EXE导出函数三种不同的解决办法(原理可能是一样的,:)): 1)在导出函数声明之前加上__declspec(dllexport)。例: __declspec(dllexport) int Add(int a, int b); __declspec(dllexport) int Su

    转自:http://bbs.pediy.com/showthread.php?t=56840


    EXE导出函数三种不同的解决办法(原理可能是一样的,:)):

    1)在导出函数声明之前加上__declspec(dllexport)。例:

    __declspec(dllexport) int Add(int a, int b);
    __declspec(dllexport) int Sub(int a, int b);
    __declspec(dllexport) int Mul(int a, int b);
    __declspec(dllexport) int Div(int a, int b);


    2)在链接器参数中设置。例:
    #pragma comment(linker, "/EXPORT:_Add,@1,NONAME")
    #pragma comment(linker, "/EXPORT:_Sub,@2,NONAME")
    #pragma comment(linker, "/EXPORT:_Mul,@3,NONAME")
    #pragma comment(linker, "/EXPORT:_Div,@4,NONAME")


    3)添加一个def文件,例:
    EXPORTS
      Add       @1  NONAME
      Sub       @2  NONAME
      Mul       @3  NONAME
      Div       @4  NONAME



    另需要在链接器命令行参数中指定def文件名:
    /DEF:Callee.def
    注意:在def文件中 不要有
    LIBRARY [library][BASE=address]

    这样的语句。

    相比较而言,后两种方法可以设置更多的参数。


    函数举例:

    extern "C" 
    {
    
    
      int Add(int a, int b)
      {
        return (a + b);
      }
    
    
      int Sub(int a, int b)
      {
        return (a - b);
      }
    
    
      int Mul(int a, int b)
      {
        return (a * b);
      }
    
    
      int Div(int a, int b)
      {
        if (b == 0)
          return 0;
        else
          return (a / b);
      }
    
    
    }
    编译时会自动生成相应的导出库(lib)文件,供调用者使用。


    调用方法和普通的动态链接库调用一样。
    调用者必须能够找到被调用者的位置,否则报错,被调用者是否运行不影响。


    调用代码举例:
    extern "C"
    {
      int Add(int a, int b);
      int Sub(int a, int b);
      int Mul(int a, int b);
      int Div(int a, int b);
    }
    
    
    #pragma comment (lib, "Callee.lib")
    
    
    void CCallerDlg::OnBnClickedCalculate()
    {
      // TODO: Add your control notification handler code here
      UpdateData(TRUE);
    
    
      switch (((CComboBox *)GetDlgItem(IDC_COMBO_OPERATOR))->GetCurSel())
      {
      case ADD:
        {
          m_iResult = Add(m_iNum1, m_iNum2);
          break;
        }
      case SUB:
        {
          m_iResult = Sub(m_iNum1, m_iNum2);
          break;
        }
      ...
      ...

    根据导出序号调用dll函数

    GetProcAddress(hDll,MAKEINTRESOURCE(sequence_num));


    另外的方式:《编程高手箴言》一书所述调用门

    //------------------------------------------------------------------------------------------------------------------------

    def文件说明传送门:

    http://andylin02.iteye.com/blog/441300

    展开全文
  • node 实现文件导出Node has a built-in module system. Node具有内置的模块系统。 A Node.js file can import functionality exposed by other Node.js files. Node.js文件可以导入其他Node.js文件公开的功能。...

    node 实现文件导出

    Node has a built-in module system.

    Node具有内置的模块系统。

    A Node.js file can import functionality exposed by other Node.js files.

    Node.js文件可以导入其他Node.js文件公开的功能。

    When you want to import something you use

    当您想导入某些东西时

    const library = require('./library')

    to import the functionality exposed in the library.js file that resides in the current file folder.

    导入位于当前文件夹中的library.js文件中公开的功能。

    In this file, functionality must be exposed before it can be imported by other files.

    在此文件中,必须先公开功能,然后才能由其他文件导入。

    Any other object or variable defined in the file by default is private and not exposed to the outer world.

    默认情况下,文件中定义的任何其他对象或变量都是私有的,不会暴露给外界。

    This is what the module.exports API offered by the module system allows us to do.

    这就是module系统提供的module.exports API允许我们执行的操作。

    When you assign an object or a function as a new exports property, that is the thing that’s being exposed, and as such, it can be imported in other parts of your app, or in other apps as well.

    当您将对象或函数分配为新的exports属性时,这就是要公开的内容,因此,可以将其导入应用程序的其他部分或其他应用程序中。

    You can do so in 2 ways.

    您可以通过两种方式进行操作。

    The first is to assign an object to module.exports, which is an object provided out of the box by the module system, and this will make your file export just that object:

    首先是将一个对象分配给module.exports ,这是模块系统提供的开箱即用的对象,这将使文件导出该对象

    const car = {
      brand: 'Ford',
      model: 'Fiesta'
    }
    
    module.exports = car
    
    //..in the other file
    
    const car = require('./car')

    The second way is to add the exported object as a property of exports. This way allows you to export multiple objects, functions or data:

    第二种方法是将导出的对象添加为一个属性exports 。 通过这种方式,您可以导出多个对象,函数或数据:

    const car = {
      brand: 'Ford',
      model: 'Fiesta'
    }
    
    exports.car = car

    or directly

    或直接

    exports.car = {
      brand: 'Ford',
      model: 'Fiesta'
    }

    And in the other file, you’ll use it by referencing a property of your import:

    在另一个文件中,您将通过引用导入的属性来使用它:

    const items = require('./items')
    items.car

    or

    要么

    const car = require('./items').car

    What’s the difference between module.exports and exports?

    module.exportsexports什么区别?

    The first exposes the object it points to. The latter exposes the properties of the object it points to.

    第一个公开它指向的对象。 后者公开它指向的对象的属性

    翻译自: https://flaviocopes.com/node-export-module/

    node 实现文件导出

    展开全文
  • 该模块导出一个类,即MTA类。 实例化该类要求您指定MTA:SA服务器的主机和端口。 句法: MTA ( host = 'localhost' , port = 22005 , username = '' , password = '' ) // Contrustor MTA . prototype . call ( ...
  • node 加载内置函数

    千次阅读 2013-11-29 19:32:18
    部分参考... 1 入口文件与入口函数 src/node_main.cc->main->node::Start->node.cc:Start //如无特别说X->Y-Z. 这里的X、Y、Z可能是两种情况。一个是C++、Javascript文件名(加后缀的),
  • Node 导入导出模块module.exports

    千次阅读 2019-08-16 09:25:17
    node.js导入导出模块module.exports 导出单个模块 // user.js moudel.exports = 函数名或者变量名; //app.js 导入 require('user.js') 当然.js可以省略 require('user'); 导出多个模块 //user.js var ...
  • app.js代码: //导包 var express = require('express'); var router = require('./router'); //创建服务器 var app = express(); //路由 router(app); //监听 app.listen(3000,function () { c...
  • linux x-executable可以像动态库一样导出函数吗? 如果不能那node c++ addon在linux下又是怎么做到的。 其实很简单,就是一个选项而已。
  • //只支持一个函数function name(name){ return name;}module.exports = name;// 支持两个及以上函数module.exports = { name:function(name){ return name; }, age:function(age){ return age; }}调用var...
  • Node中exports导出规则

    2019-03-25 15:58:13
    我们可以把需要导出的成员都挂载到 module.exports 接口对象中 也就是:moudle.exports.xxx = xxx 的方式 但是每次都 moudle.exports.xxx = xxx 很麻烦,点儿的太多了 所以 Node 为了你方便,同...
  • Node.JS中模块的导出

    2019-08-11 22:57:15
    Node.JS中模块的导出 //定义模块中的变量 var str="你好,我是一个模块" //定义模块函数 function print_str() { return '我是一个模块函数的返回值' } //定义模块函数 function square(num) { return num*num }...
  • 第一种方式导入导出 var fun = function(a, b){ return parseInt(a) + parseInt(b) } exports.fun = fun var module = require('./xxxx.js') var ret = module.fun(1, 2) 第二种方式导入导出 var fun = function...
  • Postgres 存储过程到 Node 函数的自动包装器 入门 pgproc = require('pgproc') pgproc('postgresql://localhost:5432/dbname', 'public') my_stored_procedure('call as if javascript', function(rows) { ...
  • node.js中函数的两种封装方式

    千次阅读 2016-11-03 21:24:16
    欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: ...导入导出Markdown文件 丰富的快捷键 快捷键 加粗 Ctrl + B 斜体 Ctrl + I 引用 Ctrl
  • 如果需要获取一个函数中异步操作的结果,则必须通过回调函数来获取 上层定义下层调用
  • 节点lambda函数 ... 将SQL导出到csv文件中,通过邮件发送并将其保存在S3上。 该函数触发由Cloudwatch生成的每个事件,必须将其配置为发送带有以下json数据的事件: { " query " : " SELECT * FROM db.users
  • 服务器端模块化规范CommonJS与实现Node.js 模块导出与引入 模块导出机制分析 模块加载规则 模块查找 不加扩展名的时候会按照如下后缀顺序进行查找 .js .json .node 模块分类 自定义模块 系统核心模块 fs 文件...
  • 学习node第二次笔记 两个文件: ①n4_router.js ②router.js 文件内容: n4_router.js: var http=require('http'); var url=require('url'); var router=require("./router-1.js");//我的是在根...
  • 1:配置好环境变量后 命令行REPL方式。 1:在cmd中敲node,可以在里面编译代码。 2:_表示上次执行的结果 3:.exit退出这个REPL环境。
  • nodejs调用函数主要是调用本js内部的函数,还有就是外部的函数,而调用外部函数主要的方式就是调用的函数开放可以让主函数调用,废话不说,下面开始详细讲解node.js的调用函数的具体使用。 内容 环境 下载node-v...
  • node-xlsx[依赖js-xlsx的Excel文件解析器/生成器] npm官方正规不骗人地址:https://www.npmjs.com/package/node-xlsx 之前写过的一个js-xlsx,发现最后读取完的数据格式不好用,跟需要实现的功能有偏差,听同事说...
  • 本来是想用来导出Excel的,没想到把Array的find函数能乱了。这种基础函数弄乱了,还了得吗?整个框架出现了非常严重的问题!引入包还是要慎重啊! 其实这个issue已经有人提出来了:...
  • node.js 没有全局作用域,只有模块(文件)作用域,比如:在 a 模块中引入了 b 模块,两模块之间有同名的变量但这两个变量之间互不影响,只在自己的文件中起作用;就是说外部访问不到内部,内部也无法访问到外部,...
  • node.js中的回调函数In Node.js, how to import functions from another JavaScript file like source common.sh in Bash? 在Node.js中 ,如何从另一个JavaScript文件(如Bash中的source common.sh导入函数? In ...
  • node.js 1.node.js node.js是运行在代码环境上的语言; javascript代码运行在node中,node.js是语言,node是运行环境; 2.node.js组成 js由三部分,ECMAScript,DOM,BOM; node.js由ECMAScript和node环境下的API; 3.node.js...
  • Node学习心得

    2017-06-24 12:47:17
    疑问:不使用导出函数声明,为什么使用该函数不报错 轮询机制执行 Node.js可以在不新增额外线程的情况下,依然可以对任务进行并行处理 —— Node.js是单线程的。它通过事件轮询(event loop)来实现并行操作,...
  • Node + FFmpeg 实现Canvas动画导出视频

    千次阅读 2017-04-03 07:07:05
    Canvas为前端提供了动画展示的平台,随着现在视频娱乐的流行,你是否想过把Canvas动画导出视频?目前纯前端的视频编码转换(例如WebM Encoder Whammy)还存在许多限制,较为成熟的方案是将每帧图片传给后端实现,由...
  • 1.Node.js全局对象global 在浏览器 JavaScript 中,通常 window 是全局对象, 而 Node.js 中的全局对象是 global,所有全局变量(除了 global 本身以外)都是 global 对象的属性。 注意: 最好不要使用 var 定义变量...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 32,003
精华内容 12,801
关键字:

node导出函数