added seed, and a template for serving the html

This commit is contained in:
notnull 2019-02-07 20:59:24 -08:00
parent 1a2a77beca
commit 2ac6cc2ff2
8 changed files with 113 additions and 68 deletions

3
.prettierignore Normal file
View File

@ -0,0 +1,3 @@
footer.html
table.html

View File

@ -1,35 +1,42 @@
const router = require('express').Router()
const { Article } = require('../db/models')
const buildPage = require('./buildPage')
module.exports = router
router.get('/', async (req, res, next) => {
try {
const articles = await Article.findAll()
res.status(201).send(articles)
} catch (err) {
next(err)
}
try {
const articles = await Article.findAll()
const tbl = articles
.map(
article => `<tr><td>${article.title}</td><td>${article.link}</td></tr>`
)
.join()
const page = buildPage(tbl)
console.log(page)
res.status(201).send(page)
} catch (err) {
next(err)
}
})
router.get('/:id', async (req, res, next) => {
try {
const article = await Article.findById(req.params.id)
console.log(article.title)
console.log("by: " + article.author)
console.log(article.text)
res.status(201).send(article)
} catch (err) {
next(err)
}
try {
const article = await Article.findById(req.params.id)
console.log(article.title)
console.log('by: ' + article.author)
console.log(article.text)
res.status(201).send(article)
} catch (err) {
next(err)
}
})
router.post('/', async (req, res, next) => {
const body = req.body
try {
const article = await Article.create(body)
res.redirect(article.id)
} catch (err) {
next(err)
}
const body = req.body
try {
const article = await Article.create(body)
res.redirect(article.id)
} catch (err) {
next(err)
}
})

22
api/buildPage.js Normal file
View File

@ -0,0 +1,22 @@
module.exports = listString =>
`
<!DOCTYPE html>
<html>
<body>
<h2>Haxor Newz</h2>
<h3>"uber l337"</h3>
<table>
<thead>
<th>Title</th>
<th>Link</th>
</thead>
${listString}
<tfoot>
ATTACK!
</tfoot>
</table>
<footer>&#9398; anarchy planet</footer>
</body>
</html>
`

View File

@ -2,19 +2,13 @@ const Sequelize = require('sequelize')
const db = require('../db')
const Article = db.define('articles', {
title: {
type: Sequelize.STRING,
allowNull: false
},
author: {
type: Sequelize.STRING,
defaultValue: "anonymous"
},
text: {
type: Sequelize.TEXT,
allowNull: false
},
title: {
type: Sequelize.STRING,
allowNull: false
},
link: {
type: Sequelize.STRING
}
})
module.exports = Article

View File

@ -1,37 +1,28 @@
const db = require('../db')
const fs = require("fs")
const fs = require('fs')
const { Item, Article } = require('./models')
const desert = fs.readFileSync("/home/notnull/projex/bootstrap/server/db/desert.txt", "utf8")
const testItem = {
name: 'item'
}
const testArticle = {
title: "Desert",
text: desert
title: 'read desert',
link: 'https://readdesert.org'
}
console.log(Article)
console.log(Article)
async function runSeed() {
await db.sync({ force: true })
console.log('db synced!')
console.log('seeding...')
try {
await Item.create(testItem)
await Article.create(testArticle)
console.log('seeded successfully')
} catch (err) {
console.error(err)
process.exitCode = 1
} finally {
console.log('closing db connection')
await db.close()
console.log('db connection closed')
}
await db.sync({ force: true })
console.log('db synced!')
console.log('seeding...')
try {
await Article.create(testArticle)
console.log('seeded successfully')
} catch (err) {
console.error(err)
process.exitCode = 1
} finally {
console.log('closing db connection')
await db.close()
console.log('db connection closed')
}
}
runSeed()

View File

@ -13,12 +13,8 @@ app.use(express.urlencoded({ extended: true }))
app.use(require('body-parser').text())
app.use('/api', require('./api'))
if (process.env.NODE_ENV === 'production') {
// Express will serve up production assets
app.use(express.static(path.join(__dirname, '..', 'client', 'build')))
}
app.get('*', (req, res) =>
res.sendFile(path.resolve(__dirname, '..', 'client', 'public', 'index.html'))
res.sendFile(path.resolve(__dirname, 'public', 'articles.html'))
)
// error handling endware

16
public/add.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<body>
<h2>Haxor Newz</h2>
<h3>"uber l337"</h3>
<form action="" method="POST">
Title:<br />
<input type="text" name="title" value="" /> <br />
Link:<br />
<input type="text" name="link" value="" /> <br />
</form>
<p>&#9398; anarchy planet</p>
</body>
</html>

16
public/index.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<body>
<h2>Haxor Newz</h2>
<h3>"uber l337"</h3>
<form action="" method="POST">
Title:<br />
<input type="text" name="title" value="" /> <br />
Link:<br />
<input type="text" name="link" value="" /> <br />
</form>
<footer>&#9398; anarchy planet</footer>
</body>
</html>