Tec4Gen

Untitled

May 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. let express = require("express");
  2. let app = express();
  3. let bodyParser = require("body-parser");
  4. let urlencodedParser = bodyParser.urlencoded({ extended: false });
  5. let passwordHash = require('password-hash');
  6.  
  7. app.set('view engine', 'ejs');
  8.  
  9. app.get('/', (req,res) => {
  10. res.writeHead(302, {
  11. 'Location': '/login'
  12. });
  13. res.end();
  14. });
  15.  
  16. app.get('/login', (req,res) => {
  17. res.sendFile(__dirname + '/login.html');
  18. });
  19.  
  20. app.post('/login', urlencodedParser, function (req, res) {
  21. let data = req.body;
  22. if (!data) return res.sendStatus(400);
  23. if (data.type == null || data.type == undefined) return res.sendStatus(400);
  24. switch (data.type) {
  25. case 'login':
  26.  
  27. break;
  28. case 'register':
  29. if (data.email == null || data.password == null || data.name == null) {
  30. return res.sendStatus(400);
  31. }
  32. else
  33. { console.log('Т1111111');
  34. if(main(data) != 0 ) {
  35. console.log('Такой маил есть');
  36. }else{
  37. let hPassword = passwordHash.generate(data.password);
  38. data.hPassword = hPassword;
  39. main_insert(data);
  40. }
  41. }
  42. break;
  43.  
  44. default: return res.sendStatus(400);
  45.  
  46. }
  47.  
  48.  
  49.  
  50. res.send('welcome, ' + data.email);
  51. console.log(req.body);
  52. });
  53.  
  54. app.listen(3000);
  55. let mysql= require('mysql');
  56. var connection = mysql.createConnection({
  57. host : "a0300059.xsph.ru",
  58. user : "a0300059_WTbot",
  59. password : "UxCH3vpu",
  60. database : "a0300059_WTbot",
  61. });
  62.  
  63. async function main(data){
  64. let res = await dbQuery('SELECT email FROM `WTBotClients` WHERE `email`= ?',data.email)
  65.  
  66. console.log(res);
  67. console.log(res.length);
  68. return res.length;
  69. }
  70.  
  71. async function main_insert(data){
  72. let res = await dbQuery('INSERT INTO `WTBotClients`(`name`, `menu`,`email`,`hPassword`) VALUES (?,?,?,?)', [data.name,data.password,data.email,data.hPassword]);
  73. console.log(res);
  74. }
  75.  
  76.  
  77.  
  78. async function dbQuery(...args) {
  79. return new Promise((resolve, reject) => {
  80. connection.query(args[0], args[1], (err, res) => {
  81. if (err) return reject(new Error(err));
  82. return resolve(res)
  83. }, args[3]);
  84. })
  85. }
Add Comment
Please, Sign In to add comment