【问题标题】:Creating a function in Node.js to access to ArangoDB在 Node.js 中创建一个函数来访问 ArangoDB
【发布时间】:2018-01-04 13:45:47
【问题描述】:

我是 arangodb 和 node 的新手。我可以从节点服务器连接到数据库,并成功地接收来自集合甚至边缘集合的查询或 ID。所有这些都是通过直接运行 server.js 文件来完成的。

现在我想在 node.js 中创建函数,以便能够从 Arangodb 接收每个单独任务的数据,以便以后可以将其与前端 (Angular4) 一起使用。我附上了我的 server.js 文件。谁能建议怎么做?

// BASE SETUP
// ## Assigning the values

const arangojs = require('arangojs');
const aqlQuery = arangojs.aqlQuery;
const now = Date.now();

// ## Const variables for connecting to ArangoDB database

const host = '192.000.00.000'
const port = '8529'
const username = 'xyz'
const password = 'xyz'
const path = '/_db/sgcdm_app/_api/'
const database = 'sgcdm_app'

// ## Connection to ArangoDB

db = new arangojs.Database({
url: http://${host}:${port},
databaseName: database
});
db.useBasicAuth(username, password);

const collection = db.edgeCollection('included_in');
const edge = collection.edge('included_in/595783');

//============

//## call the packages we need

var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');

//## configure app to use bodyParser()------// 

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port1 = process.env.PORT || 8080; // set our port


// START THE SERVER

app.listen(port1);
console.log('Magic happens on port1 ' + port1);

【问题讨论】:

    标签: node.js arangodb


    【解决方案1】:

    本质上,您想构建一个 API 供 Angular 使用。

    我建议你试试 Nodejs 的 Express 包。 Here is the basic example你可以关注。

    基本上,您只需使用 Express 创建一条路线并返回数据。 Angular 应用程序中需要数据的每个部分都可能需要不同的路由,尽管您当然可以使用路由参数来使事情动态化。

    app.get('/', function (req, res) {
      res.send(collection.all())
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多