added seed, and a template for serving the html
This commit is contained in:
parent
1a2a77beca
commit
2ac6cc2ff2
3
.prettierignore
Normal file
3
.prettierignore
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
footer.html
|
||||||
|
table.html
|
@ -1,35 +1,42 @@
|
|||||||
const router = require('express').Router()
|
const router = require('express').Router()
|
||||||
const { Article } = require('../db/models')
|
const { Article } = require('../db/models')
|
||||||
|
const buildPage = require('./buildPage')
|
||||||
module.exports = router
|
module.exports = router
|
||||||
|
|
||||||
router.get('/', async (req, res, next) => {
|
router.get('/', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const articles = await Article.findAll()
|
const articles = await Article.findAll()
|
||||||
|
const tbl = articles
|
||||||
res.status(201).send(articles)
|
.map(
|
||||||
} catch (err) {
|
article => `<tr><td>${article.title}</td><td>${article.link}</td></tr>`
|
||||||
next(err)
|
)
|
||||||
}
|
.join()
|
||||||
|
const page = buildPage(tbl)
|
||||||
|
console.log(page)
|
||||||
|
res.status(201).send(page)
|
||||||
|
} catch (err) {
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/:id', async (req, res, next) => {
|
router.get('/:id', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const article = await Article.findById(req.params.id)
|
const article = await Article.findById(req.params.id)
|
||||||
console.log(article.title)
|
console.log(article.title)
|
||||||
console.log("by: " + article.author)
|
console.log('by: ' + article.author)
|
||||||
console.log(article.text)
|
console.log(article.text)
|
||||||
res.status(201).send(article)
|
res.status(201).send(article)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err)
|
next(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.post('/', async (req, res, next) => {
|
router.post('/', async (req, res, next) => {
|
||||||
const body = req.body
|
const body = req.body
|
||||||
try {
|
try {
|
||||||
const article = await Article.create(body)
|
const article = await Article.create(body)
|
||||||
res.redirect(article.id)
|
res.redirect(article.id)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err)
|
next(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
22
api/buildPage.js
Normal file
22
api/buildPage.js
Normal 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>Ⓐ anarchy planet</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
`
|
@ -2,19 +2,13 @@ const Sequelize = require('sequelize')
|
|||||||
const db = require('../db')
|
const db = require('../db')
|
||||||
|
|
||||||
const Article = db.define('articles', {
|
const Article = db.define('articles', {
|
||||||
title: {
|
title: {
|
||||||
type: Sequelize.STRING,
|
type: Sequelize.STRING,
|
||||||
allowNull: false
|
allowNull: false
|
||||||
},
|
},
|
||||||
author: {
|
link: {
|
||||||
type: Sequelize.STRING,
|
type: Sequelize.STRING
|
||||||
defaultValue: "anonymous"
|
}
|
||||||
},
|
|
||||||
|
|
||||||
text: {
|
|
||||||
type: Sequelize.TEXT,
|
|
||||||
allowNull: false
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = Article
|
module.exports = Article
|
||||||
|
45
db/seed.js
45
db/seed.js
@ -1,37 +1,28 @@
|
|||||||
const db = require('../db')
|
const db = require('../db')
|
||||||
const fs = require("fs")
|
const fs = require('fs')
|
||||||
const { Item, Article } = require('./models')
|
const { Item, Article } = require('./models')
|
||||||
|
|
||||||
const desert = fs.readFileSync("/home/notnull/projex/bootstrap/server/db/desert.txt", "utf8")
|
|
||||||
|
|
||||||
|
|
||||||
const testItem = {
|
|
||||||
name: 'item'
|
|
||||||
}
|
|
||||||
|
|
||||||
const testArticle = {
|
const testArticle = {
|
||||||
title: "Desert",
|
title: 'read desert',
|
||||||
text: desert
|
link: 'https://readdesert.org'
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(Article)
|
console.log(Article)
|
||||||
async function runSeed() {
|
async function runSeed() {
|
||||||
await db.sync({ force: true })
|
await db.sync({ force: true })
|
||||||
console.log('db synced!')
|
console.log('db synced!')
|
||||||
console.log('seeding...')
|
console.log('seeding...')
|
||||||
try {
|
try {
|
||||||
await Item.create(testItem)
|
await Article.create(testArticle)
|
||||||
await Article.create(testArticle)
|
console.log('seeded successfully')
|
||||||
console.log('seeded successfully')
|
} catch (err) {
|
||||||
} catch (err) {
|
console.error(err)
|
||||||
console.error(err)
|
process.exitCode = 1
|
||||||
process.exitCode = 1
|
} finally {
|
||||||
} finally {
|
console.log('closing db connection')
|
||||||
console.log('closing db connection')
|
await db.close()
|
||||||
await db.close()
|
console.log('db connection closed')
|
||||||
console.log('db connection closed')
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
runSeed()
|
runSeed()
|
||||||
|
6
index.js
6
index.js
@ -13,12 +13,8 @@ app.use(express.urlencoded({ extended: true }))
|
|||||||
app.use(require('body-parser').text())
|
app.use(require('body-parser').text())
|
||||||
app.use('/api', require('./api'))
|
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) =>
|
app.get('*', (req, res) =>
|
||||||
res.sendFile(path.resolve(__dirname, '..', 'client', 'public', 'index.html'))
|
res.sendFile(path.resolve(__dirname, 'public', 'articles.html'))
|
||||||
)
|
)
|
||||||
|
|
||||||
// error handling endware
|
// error handling endware
|
||||||
|
16
public/add.html
Normal file
16
public/add.html
Normal 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>Ⓐ anarchy planet</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
16
public/index.html
Normal file
16
public/index.html
Normal 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>Ⓐ anarchy planet</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user