Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const sequelize = require('sequelize');
- const app = express();
- const db = new sequelize({
- database: 'test',
- username: 'postgres',
- password: 'test',
- host: 'localhost',
- port: 5432,
- dialect: 'postgres',
- dialectOptions: {
- ssl: false
- }
- });
- User = db.define('user',{
- username: { type: sequelize.STRING },
- balance: { type: sequelize.INTEGER },
- });
- db.authenticate()
- .then(()=> console.log("Connect to Database success!"))
- .catch(error=> console.log(error.message));
- app.post("/test", (req,res)=>{
- User.findById(1, {raw: true})
- .then(user=>{
- if(user.balance < 5000) res.json({message: "FALSE!"});
- else {
- User.update({balance:user.balance - 5000},{ where: {id : 1 }});
- res.json({message: "TRUE!"})
- }
- })
- });
- const port = 6969;
- app.listen(port,()=> console.log(`Sever stated at localhost:${port}`));
- const create = () => {
- fetch("http://localhost:6969/test",{method:"POST"})
- .then(res=>res.json())
- .then(json=>console.log(json))
- }
- for(let i=0;i<10;i++) create()
Add Comment
Please, Sign In to add comment