minor changes

This commit is contained in:
notnull 2019-03-02 04:57:25 -05:00
parent 6f5768195a
commit 8307b2960d
10 changed files with 64 additions and 63 deletions

View File

@ -14,6 +14,7 @@
"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
} }
} }

6
README.md Normal file
View File

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

View File

@ -1,14 +1,16 @@
const router = require('express').Router() const router = require('express').Router()
const path = require('path')
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.send('/n-------/nHello from Express!/n--------/n') res.json({ ascii })
} catch (err) { } catch (err) {
next(err) console.log(err)
next()
} }
}) })

View File

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

View File

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

View File

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

View File

@ -1,37 +1,25 @@
const db = require('../db') const db = require('../db')
const fs = require("fs") const { Item } = require('./models')
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)
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()

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
ALL YOUR BASE ARE BELONG TO US
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,24 +16,11 @@ 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, '..', 'client', 'build'))) app.use(express.static(path.join(__dirname, 'dist')))
} }
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,11 +993,13 @@
}, },
"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"
@ -1010,15 +1012,18 @@
}, },
"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",
@ -1121,7 +1126,8 @@
}, },
"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",
@ -1131,6 +1137,7 @@
"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"
} }
@ -1143,17 +1150,20 @@
"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"
@ -1170,6 +1180,7 @@
"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"
} }
@ -1242,7 +1253,8 @@
}, },
"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",
@ -1252,6 +1264,7 @@
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -1357,6 +1370,7 @@
"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 server" "start": "nodemon index.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",