1. 程式人生 > >使用node解析yaml檔案

使用node解析yaml檔案

這段時間學習使用node中swagger模組定義、設計API,由於某些需要,需要將API的ID、名字name、型別type、引數argument和URL傳給前端顯示。於是想到使用寫一個簡單的介面,將這些資料傳給前端。最近一直在使用node,所以決定使用node完成yaml檔案的解析,並將解析結果傳給前端。下面主要記錄一下解析過程。

yaml檔案

swagger: "2.0"
info:
  version: "0.0.1"
  title: A simple API Gateway 
# during dev, should point to your local machine
host: www.api.com:8000
# basePath prefixes all resource paths basePath: / # schemes: # tip: remove http to make production-grade - http - https # format of bodies a client can send (Content-Type) consumes: - application/json # format of the responses to the client (Accepts) produces: - application/json paths: /bookTo: # binds a127 app logic to a route
x-swagger-router-controller: ServiceController x-ID: "001" x-name: "airlineBookTo" get: description: 預訂往航班機票 deprecated: false tags: - "機票" # used as the method name of the controller operationId: airlineBookTo parameters: - name: isBuy in
: query description: 機票購買是否成功,true為成功,false為失敗 required: false type: boolean responses: "200": description: Success schema: # a pointer to a definition $ref: "#/definitions/ServiceResponse" # responses may fall through to errors default: description: Error schema: $ref: "#/definitions/ErrorResponse" /refundTo: # binds a127 app logic to a route x-swagger-router-controller: ServiceController x-ID: "002" x-name: "airlinerRefundTo" get: description: 退訂往航班機票 deprecated: false tags: - "機票" # used as the method name of the controller operationId: airlineRefundTo responses: "200": description: Success schema: # a pointer to a definition $ref: "#/definitions/ServiceResponse" # responses may fall through to errors default: description: Error schema: $ref: "#/definitions/ErrorResponse" /bookBack: # binds a127 app logic to a route x-swagger-router-controller: ServiceController x-ID: "003" x-name: "airlineBookBack" get: description: 預訂返航班機票 deprecated: true tags: - "機票" # used as the method name of the controller operationId: airlineBookBack responses: "200": description: Success schema: # a pointer to a definition $ref: "#/definitions/ServiceResponse" # responses may fall through to errors default: description: Error schema: $ref: "#/definitions/ErrorResponse" /getService: # binds a127 app logic to a route x-swagger-router-controller: ServiceController x-ID: "004" x-name: "getService" get: description: 獲取某型別API資訊 deprecated: false tags: - "API管理" # used as the method name of the controller operationId: getService parameters: - name: tags in: query description: 指定API的型別,0為機票,1為全部 required: false type: integer responses: "200": description: Success schema: # a pointer to a definition $ref: "#/definitions/ServiceResponse" # responses may fall through to errors default: description: Error schema: $ref: "#/definitions/ErrorResponse" /swagger: x-swagger-pipe: swagger_raw # complex objects have schema definitions definitions: ServiceResponse: required: - message properties: message: type: string ErrorResponse: required: - message properties: message: type: string

解析過程

1.使用yamljs模組載入yaml檔案,並將其轉為一個物件

const YAML = require('yamljs');
const fs = require("fs");
// file為檔案所在路徑
var data = YAML.parse(fs.readFileSync(file).toString());

2.使用console.log(data),打印出data物件

console.log(data);

這裡寫圖片描述

3.根據輸出的結果,很容易能提取出所需的目標資訊
例如提取host和全部的路徑資訊paths

var host = data.host;
var paths = Object.keys(data.paths);
console.log(host);
console.log(paths);

這裡寫圖片描述