Introduce API

This commit is contained in:
misund committed 2017-04-18 10:45:13 +02:00
1 parent 6da788f09a
commit fd5fbf7632
5 files changed
+86 -2

No files matched your search

+36
View File
@@ -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"
}
}
]
}
}
}
}
}
+1 -1
View File
@@ -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',
};
+1 -1
View File
@@ -1,5 +1,5 @@
{
"name": "aurora-demo-frontend",
"name": "grisefest-2017-web",
"version": "1.0.0",
"description": "SOL frontpage frontend application",
"main": "app.js",
+44
View File
@@ -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;
+4
View File
@@ -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