在 Node.js 中创建一个函数以访问 ArangoDB

Creating a function in Node.js to access to ArangoDB

我是 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);

本质上,您想构建一个 API 供 Angular 消费。

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

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

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