node.js 调用c动态库,选用哪种方法好

JavaScript012

node.js 调用c动态库,选用哪种方法好,第1张

2种方法。 一种编程的时候,直接include库文件. 编好的程序使用时为操作系统直接调用。 二种为编程的时候,使用LoadLibrary,FreeLibrary,GetProcAddress来动态装载程序。则编好的程序使用库文件时,是程序自己主动载入。

#define NODE_MODULE(modname, regfunc) \

extern "C" {\

NODE_MODULE_EXPORT node::node_module_struct modname ## _module = \

{ \

NODE_STANDARD_MODULE_STUFF, \

regfunc,\

NODE_STRINGIFY(modname) \

} \

}

NODE_MODULE(name, init)展开后就是:

extern "C" {

node::node_module_struct name_module =

{

1,//NODE_MODULE_VERSION

NULL,

__FILE__,

init,

"name"

}

}

其实就是定义了一个结构体,编译后为动态链接库 .node 文件中的一个符号,最后使用的时候由 node.cc 调用uv_dlopen和uv_dlsym动态链接模块,得到初始化函数并执行。

mod->register_func(target)

uv库封装了对动态链接文件操作的具体实现,win下实际调用 LoadLibraryExW 和 GetProcAddress,*nix下实际调用dlopen和dlsym实现上诉功能。

For this assignment, and web development in general you will need to exercise your "google-fu", your skill in using a search engine to find answers to problems.

You do not need to do much backend coding at all for Module 1. app.js should just contain hard coded response values.

You will need to add functionality to app.js, index.html, index.js and, if you wish, style.css.

https://developer.mozilla.org/en-US/docs/Tools

Bootstrap 4 style guide (example, forms): https://getbootstrap.com/docs/4.0/components/forms/

How to $.ajax: https://stackoverflow.com/a/22964077/5698848

https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_web_server

We are using NodeJS runtime to create a simple web server

We are using ExpressJS framework for our API and routing: https://expressjs.com/en/starter/hello-world.html

An Application Programming Interface (API) is essentially just an interface, we're using to serve our set of routes for the client browser JavaScript to interact using HTTP protocol to access Backend functionality.

We're creating a RESTful API: https://restfulapi.net/

HTTP Methods to consider:

In Express it's very simple to create a single "route". A route is just an endpoint you can access from your JavaScript

Here's an example in app.js:

More information here: https://expressjs.com/en/guide/routing.html

Request contains the data sent from the Frontend JavaScript

Response is what we send back to the client after they make an AJAX call

Note that res.send() should take a JavaScript Native Object, NOT a string with JSON in it

You can parse a JSON string into a native object:

The stub provides app.get() and app.port() routes for handling .ged file upload/doanload requests from the browser. All .ged files must be placed ino the uploads/ directory.

https://www.npmjs.com/package/node-ffi