Created
July 6, 2018 16:30
-
-
Save tonyspiro/5cfd8f342d456f0482db2a3be33b7bda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const serverless = require('serverless-http') | |
const express = require('express') | |
const app = express() | |
const PORT = process.env.PORT || 3000 | |
const Cosmic = require('cosmicjs') | |
const api = Cosmic() | |
const COSMIC_BUCKET = process.env.COSMIC_BUCKET || 'node-starter' | |
const COSMIC_READ_KEY = process.env.COSMIC_READ_KEY || '' | |
let stage_path = 'dev/' | |
if (process.env.STAGE === 'local') | |
stage_path = '' | |
const bucket = api.bucket({ | |
slug: COSMIC_BUCKET, | |
read_key: COSMIC_READ_KEY | |
}) | |
app.set('view engine', 'ejs') | |
app.get('/:slug?', (req, res) => { | |
let slug = req.params.slug | |
const year = (new Date().getFullYear()) | |
if (!slug) | |
slug = 'home' | |
bucket.getObject({ slug }).then(data => { | |
const page = data.object | |
res.render('pages/default', { page, year, stage_path }) | |
}).catch(err => { | |
const page = { title: 'Page not found' } | |
res.render('pages/404', { page, year, stage_path }) | |
}) | |
}) | |
module.exports.handler = serverless(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment