Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Koa = require('koa')
- const Router = require('koa-router')
- const logger = require('koa-logger')
- const body = require('koa-body')
- const crypto = require('crypto')
- const serve = require('koa-static-server')
- const pg = require('pg')
- const Pug = require('koa-pug')
- const pug = new Pug({
- viewPath: './views',
- debug: false,
- pretty: false,
- compileDebug: false
- })
- let pql = {}
- let pgconfig = {
- user: 'postgres',
- database: 'login',
- password: ' ',
- host: '192.168.0.9',
- port: 5432,
- max: 10,
- idleTimeoutMillis: 30000
- }
- //const pool = new pg.Pool(pgconfig);
- let client = new pg.Client(pgconfig);
- client.connect();
- const app = new Koa()
- const router = new Router()
- let genRandomString = function(length){
- return crypto.randomBytes(Math.ceil(length/2))
- .toString('hex')
- .slice(0,length);
- };
- let sha512 = function(password, salt){
- let hash = crypto.createHmac('sha512', salt);
- hash.update(password);
- let value = hash.digest('hex');
- console.log(value);
- return {
- salt:salt,
- passwordHash:value
- }
- }
- function saltHashPassword(userpassword){
- let salt = genRandomString(16);
- let passwordData = sha512(userpassword,salt);
- console.log('UserPassword = ' +userpassword);
- console.log('salt = ' +salt);
- return passwordData.passwordHash
- }
- let tasks = [
- { id: 1, name: 'Learn Node' },
- { id: 2, name: 'Learn Vue' }]
- router.get('/', function (c) {
- c.render('index.pug',{},{fromString: false})
- //c.body = 'test'
- console.log('Root Get');
- })
- router.get('/api/tasks', function (c) {
- c.body = { 'tasks': tasks }
- })
- router.get('/api/tasks/:id', function (c) {
- let id = parseInt(c.params.id) - 1
- c.body = { 'task': tasks[id] }
- })
- router.post('/', function(c){
- console.log(c.request.body);
- let hashEnd = saltHashPassword(c.request.body.name);
- console.log(hashEnd);
- //c.render('index.pug',{},{fromString: false})
- c.body = hashEnd
- })
- router.get('/page2',function (c){
- client.query('SELECT * FROM users', function(err, result){
- console.log(result.rows[0].username);
- pql = result.rows[0].username
- })
- c.body = pql
- //c.render('p paeg 2 yeah',{},{fromString: true})
- })
- //app.use(function *(){
- //let result = yield this.pg.db.client.query_('SELECT * FROM users')
- //this.body = result
- //})
- router.post('/api/tasks', function (c) {
- let name = c.request.body.name
- let task = {
- 'id': tasks.length + 1,
- 'name': name
- }
- tasks.push(task)
- c.status = 201
- c.body = {
- 'task': task
- }
- })
- app.use(body())
- app.use(logger())
- app.use(router.routes())
- //app.use(pg('postgres://postgres: @192.168.0.9:5432/login'))
- pug.use(app)
- app.listen(4000)
- console.log('Koa listening on port 4000')
- // => koajs.com
- // => learnxinyminutes.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement