NodeJS C++ JavaScript
hmm
- node
Moinmoin,
ich habe in meinem NodeJs Projekt folgendes gemacht:
per Konsole:
und dann folgendes Files im gleichen Ordner angelegt:
#include <node.h>
#include <v8.h>
using namespace v8;
Handle<Value> Method(const Arguments& args) {
HandleScope scope;
return scope.Close(String::New("world"));
}
void init(Handle<Object> target) {
target->Set(String::NewSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
-binding.gyp
{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.cc" ]
}
]
}
per Konsole:
meine Linux Konsole sagt mir dann:
gyp info it worked if it ends with ok
gyp info using node-gyp@3.6.0
gyp info using node@4.7.3 | linux | x64
gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR
gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR
gyp info spawn /usr/bin/python2
gyp info spawn args [ '/home/ubuntu/.nvm/versions/node/v4.7.3/lib/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'make',
gyp info spawn args '-I',
gyp info spawn args '/home/ubuntu/workspace/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/home/ubuntu/.nvm/versions/node/v4.7.3/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/home/ubuntu/.node-gyp/4.7.3/include/node/common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=/home/ubuntu/.node-gyp/4.7.3',
gyp info spawn args '-Dnode_gyp_dir=/home/ubuntu/.nvm/versions/node/v4.7.3/lib/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=node.lib',
gyp info spawn args '-Dmodule_root_dir=/home/ubuntu/workspace',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'build',
gyp info spawn args '-Goutput_dir=.' ]
gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory `/home/ubuntu/workspace/build'
CXX(target) Release/obj.target/hello/hello.o
../hello.cc:6:28: error: ‘Arguments’ does not name a type
Handle<Value> Method(const Arguments& args) {
^
../hello.cc:6:39: error: ISO C++ forbids declaration of ‘args’ with no type [-fpermissive]
Handle<Value> Method(const Arguments& args) {
^
In file included from /home/ubuntu/.node-gyp/4.7.3/include/node/node.h:42:0,
from ../hello.cc:1:
/home/ubuntu/.node-gyp/4.7.3/include/node/v8.h: In function ‘v8::Handle<v8::Value> Method(const int&)’:
/home/ubuntu/.node-gyp/4.7.3/include/node/v8.h:885:13: error: ‘v8::HandleScope::HandleScope()’ is protected
V8_INLINE HandleScope() {}
^
../hello.cc:7:15: error: within this context
HandleScope scope;
^
../hello.cc:8:16: error: ‘class v8::HandleScope’ has no member named ‘Close’
return scope.Close(String::New("world"));
^
../hello.cc:8:22: error: ‘New’ is not a member of ‘v8::String’
return scope.Close(String::New("world"));
^
../hello.cc: In function ‘void init(v8::Handle<v8::Object>)’:
../hello.cc:12:15: error: ‘NewSymbol’ is not a member of ‘v8::String’
target->Set(String::NewSymbol("hello"),
^
../hello.cc:13:35: error: no matching function for call to ‘v8::FunctionTemplate::New(v8::Handle<v8::Value> (&)(const int&))’
FunctionTemplate::New(Method)->GetFunction());
^
../hello.cc:13:35: note: candidate is:
In file included from /home/ubuntu/.node-gyp/4.7.3/include/node/node.h:42:0,
from ../hello.cc:1:
/home/ubuntu/.node-gyp/4.7.3/include/node/v8.h:4349:34: note: static v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate*, v8::FunctionCallback, v8::Local<v8::Value>, v8::Local<v8::Signature>, int)
static Local<FunctionTemplate> New(
^
/home/ubuntu/.node-gyp/4.7.3/include/node/v8.h:4349:34: note: no known conversion for argument 1 from ‘v8::Handle<v8::Value>(const int&) {aka v8::Local<v8::Value>(const int&)}’ to ‘v8::Isolate*’
../hello.cc: In function ‘v8::Handle<v8::Value> Method(const int&)’:
../hello.cc:9:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
make: *** [Release/obj.target/hello/hello.o] Error 1
make: Leaving directory `/home/ubuntu/workspace/build'
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/home/ubuntu/.nvm/versions/node/v4.7.3/lib/node_modules/node-gyp/lib/build.js:285:23)
gyp ERR! stack at emitTwo (events.js:87:13)
gyp ERR! stack at ChildProcess.emit (events.js:172:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:12)
gyp ERR! System Linux 4.9.17-c9
gyp ERR! command "/home/ubuntu/.nvm/versions/node/v4.7.3/bin/node" "/home/ubuntu/.nvm/versions/node/v4.7.3/bin/node-gyp" "configure" "build"
gyp ERR! cwd /home/ubuntu/workspace
gyp ERR! node -v v4.7.3
gyp ERR! node-gyp -v v3.6.0
gyp ERR! not ok
Ich wollte einfach nur ein Tutorial nachbauen um zu gucken wie ich per javaSAcript C++ Funktionen aufrufen kann. Könnt ihr mir sagen wo der Fehler ist oder habt ihr ein anderes Beispiel für mich?
Moin,
die Meldungen des C++-Compilers deuten darauf hin, dass in deiner C++-Datei mindestens ein Include fehlt.
Viele Grüße
Robert
jau, dass wars danke.
Nächste Frage: Wie finde ich die Url die zu diesem Service gehört:
var express = require("express");
var app = express();
const addon = require('./build/Release/hello');
console.log("antwort: " + addon.hello());
app.get('/addon', function(req, res){
res.send(addon.hello());
});
Muss ich "routes" definieren oder sowas? Ich kann den Url der Seite nach beliebene um "/addon" erweitern aber ich krieg den Service trotzdem nicht aufgerufen
und das hier schreibt mir nur "test" in die Konsole:
<script>
console.log("test");
$.get("/addon", function(data){
console.log("Data: " + data );
});
</script>
mit welchem Konsolenbefehl (node test.js oder npm start etc.) muss ich den folgenden Code starten um per URL die GET Methode auszulösen?
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(8080)