Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. const Koa = require('koa')
  2. const Router = require('koa-router')
  3. const logger = require('koa-logger')
  4. const body = require('koa-body')
  5. const crypto = require('crypto')
  6. const serve = require('koa-static-server')
  7. const pg = require('pg')
  8. const Pug = require('koa-pug')
  9. const pug = new Pug({
  10. viewPath: './views',
  11. debug: false,
  12. pretty: false,
  13. compileDebug: false
  14. })
  15. let pql = {}
  16. let pgconfig = {
  17. user: 'postgres',
  18. database: 'login',
  19. password: ' ',
  20. host: '192.168.0.9',
  21. port: 5432,
  22. max: 10,
  23. idleTimeoutMillis: 30000
  24. }
  25. //const pool = new pg.Pool(pgconfig);
  26. let client = new pg.Client(pgconfig);
  27. client.connect();
  28.  
  29. const app = new Koa()
  30. const router = new Router()
  31. let genRandomString = function(length){
  32. return crypto.randomBytes(Math.ceil(length/2))
  33. .toString('hex')
  34. .slice(0,length);
  35. };
  36. let sha512 = function(password, salt){
  37. let hash = crypto.createHmac('sha512', salt);
  38. hash.update(password);
  39. let value = hash.digest('hex');
  40. console.log(value);
  41. return {
  42. salt:salt,
  43. passwordHash:value
  44. }
  45. }
  46. function saltHashPassword(userpassword){
  47. let salt = genRandomString(16);
  48. let passwordData = sha512(userpassword,salt);
  49. console.log('UserPassword = ' +userpassword);
  50. console.log('salt = ' +salt);
  51. return passwordData.passwordHash
  52.  
  53. }
  54. let tasks = [
  55. { id: 1, name: 'Learn Node' },
  56. { id: 2, name: 'Learn Vue' }]
  57.  
  58. router.get('/', function (c) {
  59. c.render('index.pug',{},{fromString: false})
  60. //c.body = 'test'
  61. console.log('Root Get');
  62. })
  63.  
  64. router.get('/api/tasks', function (c) {
  65. c.body = { 'tasks': tasks }
  66. })
  67.  
  68. router.get('/api/tasks/:id', function (c) {
  69. let id = parseInt(c.params.id) - 1
  70. c.body = { 'task': tasks[id] }
  71. })
  72.  
  73. router.post('/', function(c){
  74. console.log(c.request.body);
  75. let hashEnd = saltHashPassword(c.request.body.name);
  76. console.log(hashEnd);
  77. //c.render('index.pug',{},{fromString: false})
  78. c.body = hashEnd
  79.  
  80. })
  81.  
  82. router.get('/page2',function (c){
  83. client.query('SELECT * FROM users', function(err, result){
  84. console.log(result.rows[0].username);
  85. pql = result.rows[0].username
  86. })
  87. c.body = pql
  88. //c.render('p paeg 2 yeah',{},{fromString: true})
  89. })
  90.  
  91. //app.use(function *(){
  92. //let result = yield this.pg.db.client.query_('SELECT * FROM users')
  93. //this.body = result
  94. //})
  95.  
  96. router.post('/api/tasks', function (c) {
  97. let name = c.request.body.name
  98. let task = {
  99. 'id': tasks.length + 1,
  100. 'name': name
  101. }
  102. tasks.push(task)
  103. c.status = 201
  104. c.body = {
  105. 'task': task
  106. }
  107. })
  108.  
  109. app.use(body())
  110. app.use(logger())
  111. app.use(router.routes())
  112. //app.use(pg('postgres://postgres: @192.168.0.9:5432/login'))
  113. pug.use(app)
  114. app.listen(4000)
  115.  
  116. console.log('Koa listening on port 4000')
  117.  
  118. // => koajs.com
  119. // => learnxinyminutes.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement