From fd5fbf76328f2399c41d5f611987b33f97ad98dd Mon Sep 17 00:00:00 2001 From: misund Date: Tue, 18 Apr 2017 10:45:13 +0200 Subject: [PATCH] Introduce API --- api/routes/index.json | 36 +++++++++++++++++++++++++++++++++++ config.js | 2 +- package.json | 2 +- src/api-handler.js | 44 +++++++++++++++++++++++++++++++++++++++++++ src/server-app.js | 4 ++++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 api/routes/index.json create mode 100644 src/api-handler.js diff --git a/api/routes/index.json b/api/routes/index.json new file mode 100644 index 0000000..39456cc --- /dev/null +++ b/api/routes/index.json @@ -0,0 +1,36 @@ +{ + "meta": { + "pagination": { + "hasMore": false + }, + "version": "201704091052" + }, + "data": { + "config": { + "pageInfo": { + "title": "Den store grisefesten", + "ogDescription": "I forbindelse med ordensudnævnelse i Studentersamfundets orden Den Gyldne Gris, avholdes hvert fjerde år en Grisefest.", + "ogType": "website", + "ogSiteName": "Grisefest", + "ogUrl": "http://grisefest.no", + "lpUrl": "http://grisefest.no", + "robotsIndex": "index", + "timestamp": "201704091052" + }, + "app": { + "type": "grisefest-app", + "options": { + "modules": [ + { + "type": "commercial-dfp-ads", + "options": { + "prop1": "prop1value", + "prop2": "prop2value" + } + } + ] + } + } + } + } +} diff --git a/config.js b/config.js index 5491acc..db2fc56 100644 --- a/config.js +++ b/config.js @@ -1,7 +1,7 @@ const config = { port: process.env.PORT || 3000, hostname: process.env.HOSTNAME || 'localhost', - apiUrl: process.env.API_URL || 'http://localhost::3001', + apiUrl: process.env.API_URL || 'http://localhost:3000/api', nodeEnv: process.env.NODE_ENV || 'development', }; diff --git a/package.json b/package.json index 5017cd0..43a424e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "aurora-demo-frontend", + "name": "grisefest-2017-web", "version": "1.0.0", "description": "SOL frontpage frontend application", "main": "app.js", diff --git a/src/api-handler.js b/src/api-handler.js new file mode 100644 index 0000000..5f377c9 --- /dev/null +++ b/src/api-handler.js @@ -0,0 +1,44 @@ +const express = require('express'); +const fs = require('fs'); +const path = require('path'); + +const handleApi = function(req, res, next) { // eslint-disable-line + console.log('api route handler'); + + const route = req.params.page || 'index'; + const filename = path.join(__dirname, '..', 'api', 'routes', `${route}.json`); + const filenameWithIndexAppended = path.join(filename, 'index.json'); + console.log('filename', filename); + + const readFile = filename => new Promise((resolve, reject) => { + fs.readFile( + filename, + 'utf8', + (err, data) => { + console.log(': fs.readfile callback started'); + if (err) { + console.log(err); + return reject(new Error('could not read json file')); + } + + return resolve(JSON.parse(data)); + } + ); + }); + + return readFile(filename) + // .catch(() => readFile(filenameWithIndexAppended)) + .then((data) => { + console.log('typeof data', typeof data); + return res.json(data); + }) + .catch((err) => { + console.log('this did not work'); + return res.json(err); + }); +}; + +const apiHandler = express(); +apiHandler.get('/routes/(:page)?', handleApi); + +module.exports = apiHandler; diff --git a/src/server-app.js b/src/server-app.js index a3e59c6..cba558b 100644 --- a/src/server-app.js +++ b/src/server-app.js @@ -8,6 +8,7 @@ const cookieParser = require('cookie-parser'); const favicon = require('serve-favicon'); const useragent = require('express-useragent'); const ms = require('ms'); +const apiHandler = require('./api-handler'); /** * Environment @@ -63,6 +64,9 @@ app.use(favicon(`${__dirname}/../public/static/favicon.ico`)); */ app.use(express.static(`${__dirname}/../public`, { maxAge: ms('7 days') })); +// Serve API +app.use('/api', apiHandler); + /** * Use our server rendering function * as the main route handler