使用Nodejs

时间:2016-05-04 12:11:53

标签: node.js arangodb

我正在关注ArangoDB.com上的this tutorial。 它是关于使用node.js创建Arango数据库。我是在OS X上做的。

在节点代码中执行后:

    > Database = require('arangojs').Database;
      [Function: Database]
    > db = new Database('http://127.0.0.1:8529');
      Database {
  _connection: 
   Connection {
     config: 
      { url: 'http://127.0.0.1:8529',
        databaseName: '_system',
        arangoVersion: 20300,
        agentOptions: [Object],
        headers: [Object] },
     _request: [Function: request],
     promisify: [Function] },
  _api: 
   Route {
     _connection: 
      Connection {
        config: [Object],
        _request: [Function: request],
        promisify: [Function] },
     _path: '_api',
     _headers: undefined },
  name: '_system' }

    > db.createDatabase('mydb', function (err) {
       if (!err) console.log('Database created');
       else console.error('Failed to create database:', err);
     });

我收到了ECONNREFUSED错误:

Failed to create database: { [Error: connect ECONNREFUSED 127.0.0.1:8529]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 8529,
  request: 
   ClientRequest {
     domain: 
      Domain {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        members: [] },
     _events: { response: [Object], error: [Function] },
     _eventsCount: 2,
     _maxListeners: undefined,
     output: [],
     outputEncodings: [],
     outputCallbacks: [],
     outputSize: 0,
     writable: true,
     _last: false,
     chunkedEncoding: false,
     shouldKeepAlive: true,
     useChunkedEncodingByDefault: true,
     sendDate: false,
     _removedHeader: { 'content-length': false },
     _contentLength: null,
     _hasBody: true,
     _trailer: '',
     finished: true,
     _headerSent: true,
     socket: 
      Socket {
        _connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: null,
        _readableState: [Object],
        readable: false,
        domain: [Object],
        _events: [Object],
        _eventsCount: 10,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        destroyed: true,
        bytesRead: 0,
        _bytesDispatched: 0,
        _sockname: null,
        _pendingData: [Object],
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: [Object],
        _httpMessage: [Circular],
        read: [Function],
        _consuming: true,
        _idleNext: null,
        _idlePrev: null,
        _idleTimeout: -1 },
     connection: 
      Socket {
        _connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: null,
        _readableState: [Object],
        readable: false,
        domain: [Object],
        _events: [Object],
        _eventsCount: 10,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        destroyed: true,
        bytesRead: 0,
        _bytesDispatched: 0,
        _sockname: null,
        _pendingData: [Object],
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: [Object],
        _httpMessage: [Circular],
        read: [Function],
        _consuming: true,
        _idleNext: null,
        _idlePrev: null,
        _idleTimeout: -1 },
     _header: 'POST /_db/_system/_api/database HTTP/1.1\r\ncontent-type: application/json\r\ncontent-length: 15\r\nx-arango-version: 20300\r\nHost: 127.0.0.1:8529\r\nConnection: keep-alive\r\n\r\n',
     _headers: 
      { 'content-type': 'application/json',
        'content-length': 15,
        'x-arango-version': 20300,
        host: '127.0.0.1:8529' },
     _headerNames: 
      { 'content-type': 'content-type',
        'content-length': 'content-length',
        'x-arango-version': 'x-arango-version',
        host: 'Host' },
     _onPendingData: null,
     agent: 
      Agent {
        domain: [Object],
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: [Object],
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: true,
        maxSockets: 3,
        maxFreeSockets: 256 },
     socketPath: undefined,
     method: 'POST',
     path: '/_db/_system/_api/database',
     parser: 
      HTTPParser {
        '0': [Function: parserOnHeaders],
        '1': [Function: parserOnHeadersComplete],
        '2': [Function: parserOnBody],
        '3': [Function: parserOnMessageComplete],
        '4': null,
        _headers: [],
        _url: '',
        _consumed: false,
        socket: [Object],
        incoming: null,
        outgoing: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] } } }

不幸的是我无法本地化错误。我正在寻找类似的问题,但没有找到任何解决方案。我对这些技术没有太多经验,我现在就开始使用ArangoDB了。

如果有任何提示如何解决它和/或任何其他材料如何从ArangoDB开始,我将不胜感激。

2 个答案:

答案 0 :(得分:2)

通常,如果您需要使用节点运行多行代码,则最好创建一个包含要运行的所有代码的JavaScript文件,例如script.js。要运行该代码,请使用node script.js。这将导致节点逐行评估您的代码并且(可能)持久地保持代码运行代码(像服务器这样的东西会这样做)。

使用节点shell(也就是刚刚直接运行node)将不是持久运行代码的好策略。

总之:将任何服务器代码放在script.js中,然后在bash shell(安装了节点的地方)中使用命令node script.js运行它。

很可能代码无法启动服务器并在shell内部持久消耗端口。

答案 1 :(得分:2)

错误表明客户端无法连接,可能是ArangoDB未在localhost上的端口8529上运行,尚未启动或尚未完成启动。

本教程应该从节点shell开始工作,但您可能必须避免换行(例如在调用.之类的方法时.then之前。示例中的代码已经过格式化以便于阅读,如果您想要了解某些内容,屏幕截图可能更安全。