Guest User

Untitled

a guest
Apr 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. const express = require('express');
  2. const sequelize = require('sequelize');
  3. const app = express();
  4. const db = new sequelize({
  5. database: 'test',
  6. username: 'postgres',
  7. password: 'test',
  8. host: 'localhost',
  9. port: 5432,
  10. dialect: 'postgres',
  11. dialectOptions: {
  12. ssl: false
  13. }
  14. });
  15. User = db.define('user',{
  16. username: { type: sequelize.STRING },
  17. balance: { type: sequelize.INTEGER },
  18. });
  19. db.authenticate()
  20. .then(()=> console.log("Connect to Database success!"))
  21. .catch(error=> console.log(error.message));
  22.  
  23. app.post("/test", (req,res)=>{
  24. User.findById(1, {raw: true})
  25. .then(user=>{
  26. if(user.balance < 5000) res.json({message: "FALSE!"});
  27. else {
  28. User.update({balance:user.balance - 5000},{ where: {id : 1 }});
  29. res.json({message: "TRUE!"})
  30. }
  31. })
  32. });
  33.  
  34. const port = 6969;
  35. app.listen(port,()=> console.log(`Sever stated at localhost:${port}`));
  36.  
  37. const create = () => {
  38. fetch("http://localhost:6969/test",{method:"POST"})
  39. .then(res=>res.json())
  40. .then(json=>console.log(json))
  41. }
  42.  
  43. for(let i=0;i<10;i++) create()
Add Comment
Please, Sign In to add comment