Compare commits

..

No commits in common. "5e7a3b65d6374474e98ba3341ecd23a72a52af86" and "4de7102c30f5f0fc8898086b16c57f9aad28bf0d" have entirely different histories.

13 changed files with 117 additions and 71 deletions

View File

@ -14,7 +14,6 @@
"quotes": ["warn", "single"], "quotes": ["warn", "single"],
"semi": ["warn", "never"], "semi": ["warn", "never"],
"indent": ["warn", 2], "indent": ["warn", 2],
"no-unused-vars": ["warn"], "no-unused-vars": ["warn"]
"no-console": 0
} }
} }

View File

@ -1,6 +0,0 @@
# Server
a minimal working example for the backend of a crud app built with node, express, postgres, sequelize
# TODO
- [ ] get rid of proxy

35
api/articles.js Normal file
View File

@ -0,0 +1,35 @@
const router = require('express').Router()
const { Article } = require('../db/models')
module.exports = router
router.get('/', async (req, res, next) => {
try {
const articles = await Article.findAll()
res.status(201).send(articles)
} 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)
}
})
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)
}
})

View File

@ -1,15 +1,14 @@
const router = require('express').Router() const router = require('express').Router()
module.exports = router module.exports = router
const ascii = require('../ascii')
router.use('/items', require('./items')) router.use('/items', require('./items'))
router.use('/articles', require('./articles'))
router.get('/', async (req, res, next) => { router.get('/', async (req, res, next) => {
try { try {
res.json({ ascii }) res.send('/n-------/nHello from Express!/n--------/n')
} catch (err) { } catch (err) {
console.log(err) next(err)
next()
} }
}) })

View File

@ -12,15 +12,6 @@ router.get('/', async (req, res, next) => {
} }
}) })
router.get('/:id', async (req, res, next) => {
try {
const item = await Item.findById(req.params.id)
res.json(item)
} catch (err) {
next(err)
}
})
router.post('/', async (req, res, next) => { router.post('/', async (req, res, next) => {
try { try {
const item = await Item.create(req.body) const item = await Item.create(req.body)

1
api/postcommand Normal file
View File

@ -0,0 +1 @@
curl -d {"title":"", "text": ""} -H Content-Type: application/json -X POST http://localhost:1337/api/articles

20
db/models/article.js Normal file
View File

@ -0,0 +1,20 @@
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
},
})
module.exports = Article

View File

@ -1,3 +1,4 @@
const Item = require('./item') const Item = require('./item')
const Article = require('./article')
module.exports = { Item } module.exports = { Item, Article }

View File

@ -1,25 +1,37 @@
const db = require('../db') const db = require('../db')
const { Item } = require('./models') const fs = require("fs")
const { Item, Article } = require('./models')
const desert = fs.readFileSync("/home/notnull/projex/bootstrap/server/db/desert.txt", "utf8")
const testItem = { const testItem = {
name: 'item' name: 'item'
} }
const testArticle = {
title: "Desert",
text: desert
}
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 Item.create(testItem)
console.log('seeded successfully') await Article.create(testArticle)
} catch (err) { console.log('seeded successfully')
console.error(err) } catch (err) {
process.exitCode = 1 console.error(err)
} finally { process.exitCode = 1
console.log('closing db connection') } finally {
await db.close() console.log('closing db connection')
console.log('db connection closed') await db.close()
} console.log('db connection closed')
}
} }
runSeed() runSeed()

View File

@ -1,5 +0,0 @@
const proxy = require('http-proxy-middleware')
module.exports = app => {
app.use(proxy('/api/*', { target: 'http://localhost:1337/' }))
}

View File

@ -3,11 +3,11 @@ const path = require('path')
const app = express() const app = express()
const morgan = require('morgan') const morgan = require('morgan')
const ascii = require('./ascii') const ascii = require('./ascii')
//const proxy = require('http-proxy-middleware')
const port = process.env.PORT || 1337 const port = process.env.PORT || 1337
app.use(morgan('tiny')) app.use(morgan('tiny'))
YOU HAVE NO CHANCE TO SURVIVE MAKE YOUR TIME
HA HA HA
// body parsing middleware // body parsing middleware
app.use(express.json()) app.use(express.json())
app.use(express.urlencoded({ extended: true })) app.use(express.urlencoded({ extended: true }))
@ -16,11 +16,24 @@ app.use('/api', require('./api'))
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
// Express will serve up production assets // Express will serve up production assets
app.use(express.static(path.join(__dirname, 'dist'))) app.use(express.static(path.join(__dirname, '..', 'client', 'build')))
} }
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'public', 'index.html'))
}) // app.get('/articles', async (req, res, next) => {
// try {
// const articles = await Article.findAll()
// console.log(articles)
// res.status(201).send(articles)
// } catch (err) {
// next(err)
// }
// })
app.get('*', (req, res) =>
res.sendFile(path.resolve(__dirname, '..', 'client', 'public', 'index.html'))
)
// error handling endware // error handling endware
app.use((err, req, res, next) => { app.use((err, req, res, next) => {

28
package-lock.json generated
View File

@ -993,13 +993,11 @@
}, },
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@ -1012,18 +1010,15 @@
}, },
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true, "bundled": true
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@ -1126,8 +1121,7 @@
}, },
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true, "bundled": true
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@ -1137,7 +1131,6 @@
"is-fullwidth-code-point": { "is-fullwidth-code-point": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@ -1150,20 +1143,17 @@
"minimatch": { "minimatch": {
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
}, },
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"bundled": true, "bundled": true
"optional": true
}, },
"minipass": { "minipass": {
"version": "2.3.5", "version": "2.3.5",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@ -1180,7 +1170,6 @@
"mkdirp": { "mkdirp": {
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@ -1253,8 +1242,7 @@
}, },
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true, "bundled": true
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@ -1264,7 +1252,6 @@
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -1370,7 +1357,6 @@
"string-width": { "string-width": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",

View File

@ -15,7 +15,7 @@
}, },
"scripts": { "scripts": {
"seed": "node db/seed.js", "seed": "node db/seed.js",
"start": "nodemon index.js" "start": "nodemon server"
}, },
"repository": { "repository": {
"type": "git", "type": "git",