nodenative is a C++14 (aka C++1y) port for node.js. This is alive project of d5's node.native changed considerably for better maintenance.
Linux and OSX | Coverage Status |
---|---|
Please note that nodenative project is under heavy development.
- Basic functionality of Promise/A+ based on event pool (
native::Promise<R>
,native::Future<R>
,native::async(F, Args...)
). A Future callback may return a future object. - Thread pool (
native::worker(F, Args...)
) - TCP protocol (
native::net::Tcp
) - HTTP server integrated with
ServerPlugin
and asynchronous callbacks (native::Future<void>
) - HTTP client (
native::http::get()
) - File System I/O (
native::fs
) - Timer (
native::Timer
)
An web-server example using asynchronous callback.
#include <iostream>
#include <native/native.hpp>
using namespace native;
using namespace http;
int main() { std::shared_ptr<Loop> loop = Loop::Create();
std::shared_ptr<Server> server = Server::Create(loop);
server->get("/", [](std::shared_ptr<ServerConnection> connection) -> Future<void> {
// some initial work on the main thread
std::weak_ptr<ServerConnection> connectionWeak = connection;
ServerResponse &res = connection->getResponse();
res.setStatus(200);
res.setHeader("Content-Type", "text/plain");
// wait... I have some async work too. I will update you when I'm done.
return worker([]() {
// Some work on the thread pool to keep the main thread free
std::chrono::milliseconds time(2000);
std::this_thread::sleep_for(time);
})
.then([]() {
// and some work on the main thread to sync data and avoid race condition
std::chrono::milliseconds time(100);
std::this_thread::sleep_for(time);
})
.finally([connectionWeak]() {
// in the end send the response.
connectionWeak.lock()->getResponse().end("C++ FTW\n");
});
});
server->onError([](const Error &err) { std::cout << "error name: " << err.name(); });
if (!server->listen("0.0.0.0", 8080)) {
std::cout << "cannot start server. Check the port 8080 if it is free.\n";
return 1; // Failed to run server.
}
std::cout << "Server running at http://0.0.0.0:8080/" << std::endl;
return run();
}
nodenative requires libuv and http-parser lib to use.
- Doxygen comments from include folder
- samples
- unit-tests
To compile included sample application(webserver.cpp) first run the following command in the project directory:
git submodule update --init
then generate the build files and compile:
./build.py
make -C out
build.py will try to download build dependencies (gyp) if missing. If you prefer to download manually you can do:
$ git clone https://chromium.googlesource.com/external/gyp.git build/gyp
OR
$ svn co http://gyp.googlecode.com/svn/trunk build/gyp
By default will try to generate ninja file if possible, alternatively make file. After it will build in Debug
and Release
mode.
If you want to generate for a specific build tool use -f <buildtool>
. e.x:
./build.py -f ninja
ninja -C out/Debug/
alternatively you can set custom paths to http-parser and libuv if you dont want to use the submodules. If it is build with make in debug mode, then executables are saved to out/Debug dir.
To build documentation just run the doxygen doxyfile
command from the project root. The result document will be generated into ./out/doc/
path.
In samples dir you can see samples which use native library.
To run webserver
sample compiled by make in debug mode:
out/Debug/webserver
To run tests
compiled by make in debug mode:
out/Debug/test
Tested on
- Linux with GCC 5.3.0.
- OSX 10.10.2 with xcode7