Skip to content
Snippets Groups Projects
Commit 156b9939 authored by Markus Opolka's avatar Markus Opolka
Browse files

Lint api.js

parent 3f6ac932
No related branches found
No related tags found
No related merge requests found
......@@ -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)
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment