Merge branch 'master' of totally_not_fb/hacker-news-cli into master

This commit is contained in:
notnull 2019-02-12 01:12:54 -08:00 committed by Gogs
commit 10091f28ff
16 changed files with 3248 additions and 122 deletions

View File

@ -1,42 +1,43 @@
const router = require('express').Router()
const { Article } = require('../db/models')
const buildPage = require('./buildPage')
module.exports = router
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()
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)
.join();
const page = buildPage(tbl);
console.log(page);
res.status(201).send(page);
} catch (err) {
next(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)
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)
next(err);
}
})
});
router.post('/', async (req, res, next) => {
const body = req.body
const body = req.body;
try {
const article = await Article.create(body)
res.redirect(article.id)
const article = await Article.create(body);
res.redirect(article.id);
} catch (err) {
next(err)
next(err);
}
})
});

View File

@ -19,4 +19,4 @@ module.exports = listString =>
</body>
</html>
`
`;

View File

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

View File

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

View File

@ -1,12 +1,13 @@
const router = require('express').Router()
module.exports = router
const router = require('express').Router();
module.exports = router;
// what you will hit at /api/robots
router.get('/', async (req, res, next) => {
try {
console.log('NSA was here')
res.status(201).send('FUCK YOU NSA\n')
console.log('NSA was here');
res.status(201).send('FUCK YOU NSA\n');
} catch (err) {
next(err)
next(err);
}
})
});

View File

@ -21,6 +21,6 @@ const ascii = String.raw`
____^/\___^--____/\____O______________/\/\---/\___________--
/\^ ^ ^ ^ ^^ ^ '\ ^ ^
-- - -- - - --- __ ^
-- __ ___-- ^ ^`
-- __ ___-- ^ ^`;
module.exports = ascii
module.exports = ascii;

View File

@ -1,7 +1,8 @@
const Sequelize = require('sequelize')
const pkg = require('../package.json')
const Sequelize = require('sequelize');
const pkg = require('../package.json');
const databaseName = pkg.name + (process.env.NODE_ENV === 'test' ? '-test' : '')
const databaseName =
pkg.name + (process.env.NODE_ENV === 'test' ? '-test' : '');
const createDB = () => {
const db = new Sequelize(
@ -10,16 +11,16 @@ const createDB = () => {
logging: false,
operatorsAliases: false
}
)
return db
}
);
return db;
};
const db = createDB()
const db = createDB();
module.exports = db
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())
after('close database connection', () => db.close());
}

View File

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

View File

@ -1,5 +1,5 @@
const Sequelize = require('sequelize')
const db = require('../db')
const Sequelize = require('sequelize');
const db = require('../db');
const Article = db.define('articles', {
title: {
@ -9,6 +9,6 @@ const Article = db.define('articles', {
link: {
type: Sequelize.STRING
}
})
});
module.exports = Article
module.exports = Article;

View File

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

View File

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

View File

@ -1,28 +1,27 @@
const db = require('../db')
const fs = require('fs')
const { Item, Article } = require('./models')
const db = require('../db');
const { Article } = require('./models');
const testArticle = {
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...')
await db.sync({ force: true });
console.log('db synced!');
console.log('seeding...');
try {
await Article.create(testArticle)
console.log('seeded successfully')
await Article.create(testArticle);
console.log('seeded successfully');
} catch (err) {
console.error(err)
process.exitCode = 1
console.error(err);
process.exitCode = 1;
} finally {
console.log('closing db connection')
await db.close()
console.log('db connection closed')
console.log('closing db connection');
await db.close();
console.log('db connection closed');
}
}
runSeed()
runSeed();

View File

@ -1,20 +1,20 @@
const fetch = require("node-fetch");
const fetch = require('node-fetch');
// implemented from: https://github.com/HackerNews/API
const HN_PREFIX = "https://hacker-news.firebaseio.com/v0/";
const HN_PREFIX = 'https://hacker-news.firebaseio.com/v0/';
const TOP_STORIES = "topstories";
const ITEM = "item";
const TOP_STORIES = 'topstories';
const ITEM = 'item';
function hnFetch(type, id = "") {
function hnFetch(type, id = '') {
const url = id
? `${HN_PREFIX}${type}/${id}.json`
: `${HN_PREFIX}${type}.json`;
return fetch(url, {
method: "GET",
method: 'GET',
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json'
}
})
.then(res => {
@ -36,7 +36,12 @@ async function main() {
storyIds.slice(0, 20).map(storyId => hnFetch(ITEM, storyId))
);
console.log(stories.map(story => { delete story.kids; return story; }));
console.log(
stories.map(story => {
delete story.kids;
return story;
})
);
}
main();

View File

@ -1,31 +1,41 @@
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
const express = require('express');
const path = require('path');
const morgan = require('morgan');
const ascii = require('./ascii');
const db = require('./db');
app.use(morgan('tiny'))
const app = express();
const port = process.env.PORT || 1337;
db.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
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'))
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(require('body-parser').text());
app.use('/api', require('./api'));
app.get('*', (req, res) =>
res.sendFile(path.resolve(__dirname, 'public', 'articles.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()
})
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}`)
})
console.log(`\n${ascii}\n`);
console.log(`Doin' haxor stuff on port ${port}`);
});

3083
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -18,15 +18,39 @@
"seed": "node db/seed.js",
"start": "nodemon index"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
]
},
"dependencies": {
"node-fetch": "^2.3.0",
"axios": "^0.18.0",
"body-parser": "^1.18.3",
"concurrently": "^4.0.1",
"express": "^4.16.4",
"http-proxy-middleware": "^0.19.0",
"morgan": "^1.9.1",
"node-fetch": "^2.3.0",
"nodemon": "^1.18.9",
"pg": "^7.5.0",
"sequelize": "^4.39.1"
},
"devDependencies": {
"eslint": "^5.3.0",
"eslint-config-prettier": "^4.0.0",
"eslint-config-recommended": "^4.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.12.4",
"husky": "^1.3.1",
"lint-staged": "^8.1.3",
"prettier": "^1.16.4"
}
}