MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS N API

Hello to N-API


This module register hello function on hello module. hello function prints Hello world on console with printf and return 1373 from native function into javascript caller.

#include <node_api.h>
#include <stdio.h>
napi_value say_hello(napi_env env, napi_callback_info info)
{
 napi_value retval;
 printf("Hello world\n");
 napi_create_number(env, 1373, &retval);
 return retval;
}
void init(napi_env env, napi_value exports, napi_value module, void* priv)
{
 napi_status status;
 napi_property_descriptor desc = {
 /*
 * String describing the key for the property, encoded as UTF8.
 */
 .utf8name = "hello",
 /*
 * Set this to make the property descriptor object's value property
 * to be a JavaScript function represented by method.
 * If this is passed in, set value, getter and setter to NULL (since these members won't be
used).
 */
 .method = say_hello,
 /*
 * A function to call when a get access of the property is performed.
 * If this is passed in, set value and method to NULL (since these members won't be used).
 * The given function is called implicitly by the runtime when the property is accessed
 * from JavaScript code (or if a get on the property is performed using a N-API call).
 */
 .getter = NULL,
 /*
 * A function to call when a set access of the property is performed.
 * If this is passed in, set value and method to NULL (since these members won't be used).
 * The given function is called implicitly by the runtime when the property is set
 * from JavaScript code (or if a set on the property is performed using a N-API call).
 */
 .setter = NULL,
 /*
 * The value that's retrieved by a get access of the property if the property is a data
property.
 * If this is passed in, set getter, setter, method and data to NULL (since these members
won't be used).
*/
 .value = NULL,
 /*
 * The attributes associated with the particular property. See napi_property_attributes.
 */
 .attributes = napi_default,
 /*
 * The callback data passed into method, getter and setter if this function is invoked.
 */
 .data = NULL
 };
 /*
 * This method allows the efficient definition of multiple properties on a given object.
 */
 status = napi_define_properties(env, exports, 1, &desc);
 if (status != napi_ok)
 return;
}
NAPI_MODULE(hello, init)


Conclusion

In this page (written and validated by ) you learned about NodeJS N API . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Multithreading.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.