first commit

This commit is contained in:
notnull 2019-01-25 20:08:50 -05:00
commit 12e755d2fc
13 changed files with 2596 additions and 0 deletions

19
.eslintrc Normal file
View File

@ -0,0 +1,19 @@
{
"extends": ["eslint:recommended"],
"parserOptions": {
"ecmaVersion": 8
},
"env": {
"es6": true,
"node": true
},
"rules": {
"quotes": ["warn", "single"],
"semi": ["warn", "never"],
"indent": ["warn", 2],
"no-unused-vars": ["warn"]
}
}

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# dependencies
/node_modules
# production
/build
npm-debug.log*

18
api/index.js Executable file
View File

@ -0,0 +1,18 @@
const router = require('express').Router()
module.exports = router
router.use('/items', require('./items'))
router.get('/', async (req, res, next) => {
try {
res.send('Hello from Express!')
} catch (err) {
next(err)
}
})
router.use((req, res, next) => {
const error = new Error('Not Found!!!!!!!')
error.status = 404
next(error)
})

23
api/items.js Executable file
View File

@ -0,0 +1,23 @@
const router = require('express').Router()
const { Item } = require('../db/models')
module.exports = router
router.get('/', async (req, res, next) => {
try {
const items = await Item.findAll()
res.status(201).send(items)
} catch (err) {
next(err)
}
})
router.post('/', async (req, res, next) => {
try {
const item = await Item.create(req.body)
res.status(201).json(item)
} catch (err) {
next(err)
}
})

26
ascii.js Normal file
View File

@ -0,0 +1,26 @@
const ascii = String.raw`
. .
* . . . . *
. . . . . .
o . .
. . . .
0 . anarchy
. . , planet , ,
. \ . .
. . \ ,
. o . . .
. . . \ , .
#\##\# . .
# #O##\### .
. . #*# #\##\### .
. ##*# #\##\## .
. . ##*# #o##\# .
. *# #\# . .
\ . .
____^/\___^--____/\____O______________/\/\---/\___________--
/\^ ^ ^ ^ ^^ ^ '\ ^ ^
-- - -- - - --- __ ^
-- __ ___-- ^ ^`
module.exports = ascii

25
db/db.js Executable file
View File

@ -0,0 +1,25 @@
const Sequelize = require('sequelize')
const pkg = require('../package.json')
const databaseName = pkg.name + (process.env.NODE_ENV === 'test' ? '-test' : '')
const createDB = () => {
const db = new Sequelize(
process.env.DATABASE_URL || `postgres://localhost:5432/${databaseName}`,
{
logging: false,
operatorsAliases: false
}
)
return db
}
const db = createDB()
module.exports = db
// This is a global Mocha hook used for resource cleanup.
// Otherwise, Mocha v4+ does not exit after tests.
if (process.env.NODE_ENV === 'test') {
after('close database connection', () => db.close())
}

6
db/index.js Executable file
View File

@ -0,0 +1,6 @@
const db = require('./db')
// register models
require('./models')
module.exports = db

3
db/models/index.js Executable file
View File

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

11
db/models/item.js Executable file
View File

@ -0,0 +1,11 @@
const Sequelize = require('sequelize')
const db = require('../db')
const Item = db.define('items', {
name: {
type: Sequelize.STRING,
allowNull: false
}
})
module.exports = Item

24
db/seed.js Executable file
View File

@ -0,0 +1,24 @@
const db = require('../db')
const { Item } = require('./models')
const testItem = {
name: 'item'
}
async function runSeed() {
await db.sync({ force: true })
console.log('db synced!')
console.log('seeding...')
try {
await Item.create(testItem)
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()

35
index.js Executable file
View File

@ -0,0 +1,35 @@
const express = require('express')
const path = require('path')
const app = express()
const morgan = require('morgan')
const ascii = require('./ascii')
const port = process.env.PORT || 1337
app.use(morgan('tiny'))
// body parsing middleware
app.use(express.json())
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'))
)
// error handling endware
app.use((err, req, res, next) => {
console.error(err)
console.error(err.stack)
res.status(err.status || 500).send(err.message || 'Internal server error.')
next()
})
app.listen(port, () => {
console.log('\n' + ascii + '\n')
console.log(`Doin' haxor stuff on port ${port}`)
})

2370
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

29
package.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "server",
"version": "1.0.0",
"description": "a minimalist serverside template",
"main": "index.js",
"dependencies": {
"axios": "^0.18.0",
"concurrently": "^4.0.1",
"http-proxy-middleware": "^0.19.0",
"express": "^4.16.4",
"morgan": "^1.9.1",
"pg": "^7.5.0",
"sequelize": "^4.39.1"
},
"scripts": {
"seed": "node db/seed.js",
"start": "nodemon server"
},
"repository": {
"type": "git",
"url": "git+https://irc.anarchyplanet.org/git/notnull/server.git"
},
"author": "notnull",
"license": "ISC",
"bugs": {
"url": "https://irc.anarchyplanet.org/git/notnull/server/issues"
},
"homepage": "irc.anarchyplanet.org"
}