Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
vue-cdbp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Markus Opolka
vue-cdbp
Commits
156b9939
Commit
156b9939
authored
7 years ago
by
Markus Opolka
Browse files
Options
Downloads
Patches
Plain Diff
Lint api.js
parent
3f6ac932
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
api.js
+20
-17
20 additions, 17 deletions
api.js
with
20 additions
and
17 deletions
api.js
+
20
−
17
View file @
156b9939
...
...
@@ -8,25 +8,25 @@ const app = express()
const
PORT
=
3000
const
parser
=
new
xml2js
.
Parser
()
app
.
use
(
function
(
req
,
res
,
next
)
{
res
.
header
(
"
Access-Control-Allow-Origin
"
,
"
*
"
)
res
.
header
(
"
Access-Control-Allow-Headers
"
,
"
Origin, X-Requested-With, Content-Type, Accept
"
)
app
.
use
(
function
(
req
,
res
,
next
)
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
'
*
'
)
res
.
header
(
'
Access-Control-Allow-Headers
'
,
'
Origin, X-Requested-With, Content-Type, Accept
'
)
next
()
})
/*
* Reads XML file and transforms it to JSON
*/
function
readXMLFile
(
filepath
)
{
function
readXMLFile
(
filepath
)
{
var
data
=
{}
try
{
var
xml
=
fs
.
readFileSync
(
filepath
,
'
utf8
'
)
var
xml
=
fs
.
readFileSync
(
filepath
,
'
utf8
'
)
parser
.
parseString
(
xml
,
function
(
err
,
result
)
{
data
=
result
})
}
catch
(
err
)
{
console
.
log
(
'
Error while loading
'
+
filepath
)
console
.
log
(
err
)
return
data
}
...
...
@@ -37,10 +37,10 @@ function readXMLFile(filepath) {
/*
* From example_1.xml to example 1
*/
function
transformFilename
(
filename
,
url
=
false
)
{
filename
=
filename
.
slice
(
0
,
-
4
)
function
transformFilename
(
filename
,
url
=
false
)
{
filename
=
filename
.
slice
(
0
,
-
4
)
if
(
!
url
){
if
(
!
url
)
{
filename
=
filename
.
replace
(
/_/g
,
'
'
)
}
...
...
@@ -50,26 +50,27 @@ function transformFilename(filename, url=false) {
/*
* Get all files in fithepath
*/
function
getJsonFiles
(
filepath
)
{
function
getJsonFiles
(
filepath
)
{
var
data
=
[]
try
{
files
=
fs
.
readdirSync
(
filepath
)
var
files
=
fs
.
readdirSync
(
filepath
)
}
catch
(
err
)
{
console
.
log
(
'
Error while loading files from
'
+
filepath
)
console
.
log
(
err
)
return
data
}
for
(
var
i
in
files
)
{
if
(
!
files
[
i
].
endsWith
(
'
.xml
'
))
{
continue
}
var
letter
=
{
link
:
'
/letters/
'
+
files
[
i
].
slice
(
0
,
-
4
),
link
:
'
/letters/
'
+
files
[
i
].
slice
(
0
,
-
4
),
name
:
transformFilename
(
files
[
i
]),
filepath
:
path
.
join
(
dir
,
files
[
i
]),
url
:
'
http://localhost:3000/api/letters/
'
+
transformFilename
(
files
[
i
],
url
=
true
)
url
:
'
http://localhost:3000/api/letters/
'
+
transformFilename
(
files
[
i
],
true
)
}
data
.
push
(
letter
)
...
...
@@ -79,13 +80,15 @@ function getJsonFiles(filepath) {
}
app
.
get
(
'
/api/letters
'
,
function
(
req
,
res
)
{
data
=
getJsonFiles
(
dir
)
var
data
=
getJsonFiles
(
dir
)
res
.
send
(
data
)
})
app
.
get
(
'
/api/letters/:filename
'
,
function
(
req
,
res
)
{
filename
=
req
.
params
.
filename
+
'
.xml
'
var
filename
=
req
.
params
.
filename
+
'
.xml
'
res
.
send
(
readXMLFile
(
path
.
join
(
dir
,
filename
)))
})
app
.
listen
(
PORT
)
app
.
listen
(
PORT
,
function
()
{
console
.
log
(
'
Running API on port
'
+
PORT
)
})
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment