2020-05-05 09:10:41 -04:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const { vueRenderer } = require('@doweb/vuexpress')
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
|
2020-05-07 13:27:34 -04:00
|
|
|
// ALlow URLencoded API
|
|
|
|
app.use(bodyParser.urlencoded({
|
|
|
|
extended: false
|
|
|
|
}))
|
|
|
|
|
2020-05-05 09:52:46 -04:00
|
|
|
// Allow JSON API
|
2020-05-05 09:10:41 -04:00
|
|
|
app.use(bodyParser('json'))
|
|
|
|
|
2020-05-05 09:52:46 -04:00
|
|
|
// Enable static files
|
|
|
|
app.use(express.static('public'))
|
2020-05-07 13:27:34 -04:00
|
|
|
app.use(express.static('static'))
|
2020-05-05 09:52:46 -04:00
|
|
|
|
2020-05-05 09:10:41 -04:00
|
|
|
// Set up VueXpress
|
|
|
|
let options = {
|
|
|
|
views: './views',
|
|
|
|
cache: true,
|
|
|
|
watch: process.env.NODE_ENVIRONMENT == 'dev',
|
|
|
|
metaInfo: {
|
|
|
|
title: 'Seance',
|
|
|
|
},
|
|
|
|
extractCSS: true,
|
2020-05-07 13:25:44 -04:00
|
|
|
cssOutputPath: '/css/styles.css',
|
2020-05-05 09:52:46 -04:00
|
|
|
publicPath: 'public',
|
2020-05-05 09:10:41 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderer = vueRenderer(options)
|
|
|
|
app.use(renderer)
|
|
|
|
|
|
|
|
// Views
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
2020-05-07 13:27:34 -04:00
|
|
|
res.render('index')
|
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/fetch/', (req, res) => {
|
|
|
|
res.redirect('/')
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/fetch/', (req, res) => {
|
|
|
|
var json
|
|
|
|
var post
|
|
|
|
|
|
|
|
try {
|
|
|
|
json = JSON.parse(req.body.data)
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json) {
|
|
|
|
post = json.payload.value
|
|
|
|
console.log(post)
|
|
|
|
|
|
|
|
// set author
|
|
|
|
post.author = post.displayAuthor
|
|
|
|
|
|
|
|
// If the author's not available, get it from somewhere else
|
|
|
|
// function courtesy mediumexporter
|
|
|
|
let authors = []
|
|
|
|
if (json.payload.references && json.payload.references.User) {
|
|
|
|
Object.keys(json.payload.references.User).forEach(k => {
|
|
|
|
let u = json.payload.references.User[k]
|
|
|
|
authors.push({
|
|
|
|
name: u.name,
|
|
|
|
username: u.username,
|
|
|
|
userId: u.userId
|
|
|
|
})
|
|
|
|
})
|
|
|
|
post.authors = authors
|
|
|
|
|
|
|
|
if (!post.author) {
|
|
|
|
post.author = authors[0].name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set featured image
|
|
|
|
if (post.virtuals.previewImage) {
|
|
|
|
post.featuredImage = 'https://cdn-images-1.medium.com/max/800/' + post.virtuals.previewImage.imageId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// render the final post
|
|
|
|
res.render('fetch-medium', {
|
|
|
|
post: {
|
|
|
|
title: post.title,
|
|
|
|
subtitle: post.content.subtitle,
|
|
|
|
author: post.author,
|
|
|
|
featuredImage: post.featuredImage,
|
|
|
|
mediumUrl: post.mediumUrl,
|
|
|
|
}
|
|
|
|
})
|
2020-05-05 09:10:41 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/api/', (req, res) => {
|
|
|
|
res.json({
|
|
|
|
status: 'success',
|
|
|
|
message: 'Welcome to the Seance API :)',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const port = process.env.PORT || 4000
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Listening on ${port}`)
|
|
|
|
})
|