Nodejs:如何在从API请求获得的原始文件数据中重复检查特定字符串?
我有一个使用 Nodejs 开发的应用程序。该应用程序正在向 GitLab API 发出请求并从中获取原始文件数据。
我想读取另一个字符串之后出现的特定字符串,并从中获取所有类似的数据。我只是对这部分有点困惑,无法进一步进行,有人可以向我解释如何实现这一目标吗?
以下是示例文件数据: 我想读取关键字 Scenario:
之后出现的所有数字,即在这种情况下我想获取 A001-D002 & K002-M002
。这些数字可以是任意数字,并且可以出现在文件内容中的任何位置。我想读取它们并将它们存储在该特定文件的数组中。
FileName: File Data
Background:
This is some random background
Scenario: A001-D002 My first scenario
Given I am sitting on a plane
When I am offered drinks
Scenario: K002-M002 My second scenario
Given when I book the uber taxi
When I get the notifications
我不明白如何迭代文件内容并读取每个单词并匹配并相应地获取 id。
以下是我向 GitLab 发出请求并获取原始文件内容的代码: ./index.js
:
const express = require('express');
const http = require("http");
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 9000;
const gitlabDump = require("./controller/GitLabDump");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Make NodeJS to Listen to a particular Port in Localhost
app.listen(port, function(){
gitlabDump.gitlabDump(type, function(data){
console.log("Completed Execution for GitLab")
process.exit();
})
}
我的./controller/GitLabDump.js
:
const request = require('request');
const https = require('https');
const axios = require('axios');
exports.gitlabDump = function(callback){
var gitlabAPI = "https://gitlab.com/api/v4/projects/<project_id>/repository/files/tree/<subfolders>/<fileName>/raw?ref=master&private_token=<privateToken>";
//Make the request to the each file and read its raw contents
request(gitlabAPI, function(error, response, body) {
const featureFileData = JSON.parse(JSON.stringify(body)).toString();
console.log(featureFileData)
for(const match of featureFileData.matchAll("Scenario:")){
console.log(match);
}
callback("Completed");
})
}
我能够打印文件内容。有人可以解释一下如何迭代原始文件内容并获取所有必需的 id 吗?
I have an application developing using Nodejs
. This application is making a request to GitLab API and obtaining the raw file data from it.
I would like to read the particular string which is present after another string and get all similar data from it. I am just a bit confused on this part and unable to proceed further can someone please explain to me how to achieve this?
Following is the sample file data:
I would like to read all the numbers if present after the keyword Scenario:
i.e in this case I would like to get A001-D002 & K002-M002
. These numbers can be anything random and can appear anywhere within the file content. I would like to read them and store them within an array for that particular file.
FileName: File Data
Background:
This is some random background
Scenario: A001-D002 My first scenario
Given I am sitting on a plane
When I am offered drinks
Scenario: K002-M002 My second scenario
Given when I book the uber taxi
When I get the notifications
I am not understanding how to iterate over the file content and read every word and match and accordingly obtain the ids.
Following is the code that I have which makes the request to GitLab and obtains the raw file content:./index.js
:
const express = require('express');
const http = require("http");
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 9000;
const gitlabDump = require("./controller/GitLabDump");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Make NodeJS to Listen to a particular Port in Localhost
app.listen(port, function(){
gitlabDump.gitlabDump(type, function(data){
console.log("Completed Execution for GitLab")
process.exit();
})
}
My ./controller/GitLabDump.js
:
const request = require('request');
const https = require('https');
const axios = require('axios');
exports.gitlabDump = function(callback){
var gitlabAPI = "https://gitlab.com/api/v4/projects/<project_id>/repository/files/tree/<subfolders>/<fileName>/raw?ref=master&private_token=<privateToken>";
//Make the request to the each file and read its raw contents
request(gitlabAPI, function(error, response, body) {
const featureFileData = JSON.parse(JSON.stringify(body)).toString();
console.log(featureFileData)
for(const match of featureFileData.matchAll("Scenario:")){
console.log(match);
}
callback("Completed");
})
}
I am able to print the file contents. Can someone please explain me how can I iterate over the raw file contents and get all the required ids?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用一种方法,通过迭代每一行来分析字符串的每个部分(我假设您的字符串是像您的示例中那样组成的)。与使用正则表达式相比,它更容易理解和编码。
下面的示例代表您的
请求
回调函数。我将代码分为 3 个逻辑:
之后,您可以轻松更改 ID 过滤器(
txt.substr(0, txt.indexOf(' ')
) 使用更正确的表达式来提取句子,结果将发送到回调函数,第一个参数是文件名,第二个参数是所有 id,就像您在示例中所做的那样。 。
I suggest you to use a method by analyzing each part of your string by iterating over each lines (i assume that your string is compose like in your exemple). It is easier to understand and coding it than using a regex.
The exemple below represent your
request
callback function.I split the code in 3 logics :
You can after that, easily change you ID filter (
txt.substr(0, txt.indexOf(' ')
) to use a more proper expression to extract your sentence.The result is sent to a callback function with as first argument the filename, and as second all ids. Like you did in your exemple.