Giter VIP home page Giter VIP logo

nodenative

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.

Feature highlights

  • 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)

Sample code

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();
}

Getting started

nodenative requires libuv and http-parser lib to use.

Documentation

Build code

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.

Build documentation

To build documentation just run the doxygen doxyfile command from the project root. The result document will be generated into ./out/doc/ path.

Run samples

In samples dir you can see samples which use native library.

To run webserver sample compiled by make in debug mode:

out/Debug/webserver

Run tests

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

nodenative's Projects

gn_tool icon gn_tool

downloads recent gn binaries (linux, mac, win)

nodenative icon nodenative

C++14 port for the Node.js: native performance and modern simplicity

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.