Is it possible to write a node.js extension in C (not C++)?

A quick google search yields at least one tutorial for writing a C++ "Hello World" for node.js, but it's unclear if it's possible to write such an extension using only C. Assuming it is possible, what challenges / limitations would I face?

You can write parts of your extension in C if you want, but you'll need at least a small bit of C++ code to glue together your C code with Node.

As you will have seen in your HelloWorld, extensions rely on the v8.h and node.h headers, which have all of the classes that Node expects. Without those, you won't be able to properly create the JS object to export back to Node.

That said, you can pretty easily just write a small set of C++ functions that just call C functions, and wrap some kind of C structure.

Found this on Hacker News:

https://github.com/wesolows/v8plus

v8+: Node addon C++ to C boundary

This layer offers a way to write at least simple Node addons in C without all the horrible C++ goop you'd otherwise be expected to use. That goop still exists, but you don't have to write it. More importantly, you can write your module in a sane programming environment, avoiding the confusing and error-prone C++ semantics.

Need to declare the individual C function in your C++ code using the extern "C" syntax

Example:

#define BUILDING_NODE_EXTENSION
#include <node.h>

extern "C" void f(int i, char c, float x);

using namespace v8;

if you have multiple C functions, then it can be grouped via braces:

extern "C" {
  void   f(int i, char c, float x);
  int    g(char* s, char const* s2);
  double sqrtOfSumOfSquares(double a, double b);
}

then call the function from the C++ function:

Handle<Value> MyFunction(const Arguments& args) {
  HandleScope scope;
   f(7, 'x', 3.14);    // <--- 
  return scope.Close(String::New("Hello"));
}

Handle<Value> CreateFunction(const Arguments& args) {
  HandleScope scope;

  Local<FunctionTemplate> tpl = FunctionTemplate::New(MyFunction);
  Local<Function> fn = tpl->GetFunction();
  fn->SetName(String::NewSymbol("theFunction")); // omit this to make it anonymous

  return scope.Close(fn);
}

void Init(Handle<Object> target) {
  target->Set(String::NewSymbol("createFunction"),
      FunctionTemplate::New(CreateFunction)->GetFunction());
}


NODE_MODULE(addon, Init)

Note: Using sample code from Nodejs Addons

The code that directly interacts with node.js needs to written in C++.

You could write extern "C" wrappers using opaque types for everything you need from node.h and v8.h, but it's probably easier to just use C++ instead (which, of course, can call out to C code).

Now we have at least 3 good options:

node-ffi: Node.js Foreign Function Interface
addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C code
https://github.com/node-ffi/node-ffi

SWIG: Simplified Wrapper and Interface Generator
(it generates wrappers for many languages, what solves many problems at once)
http://www.swig.org/

emscripten
Compiles C and C++ into highly-optimizable JavaScript that runs even on the web at near-native speed, without plugins.
http://kripken.github.io/emscripten-site/