Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
9.4 kB
1
Indexable
Never
main.js

const express = require('express');
const mongoose = require('mongoose')
const csv = require('csv-parser')
const fs= require('fs')
const app  =express();
const path =require('path');
const mysql = require('mysql2');
const bodyParser = require('body-parser');

app.use(express.json());
app.use(express.urlencoded({ extended: true }))
app.use(bodyParser.json());
app.use(bodyParser.raw())
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(3000)

const result =[] 
const zmq = require('zeromq') 
//fs.createReadStream('car1_data.csv').pipe(csv({})).on('data',(data)=>result.push(data)).on('end',() => {});


/*const sock = new zmq.Push();
run();
async function run () {

    await sock.bind("tcp://127.0.0.1:3000");
    console.log("Server is ready listening on port 3000");
    console.log("Press any key to start sending the jobs!")
    process.stdin.once("data", send); 
}

//sending the jobs to the workers
async function send () {
 
    console.log("About to send jobs!");
    for (let i = 0 ;i < result.length; i++) {
         
        await sock.send(JSON.stringify(result[i]));
        //wait 500ms
        await new Promise(resolve => setTimeout(resolve, 200))
    }
}*/
var connection = mysql.createConnection({
    connectionLimit:10,
    host: 'localhost',
    user: 'root',
    password: '3005',
    database: 'user'
  });
  connection.connect(function(error){
    if(error){
      console.log("err")
    }
else{
  console.log('connected')
}  })

app.get('/',(req,res)=>{
    res.sendFile(path.join(__dirname,'index.html'));
});
app.get('/map',(req,res)=>{
     
  res.sendFile(path.join(__dirname,'map.html'));
});
app.post("/submit", (req, res, next) => {

    if(req.body.password==" "&& req.body.name==" "){
        return res.status(500).json({
           message:"Make sure there isn't any empty field"
          });
    }
   else if(req.body.password==""|| req.body.name==""){
        return res.status(500).json({
           message:"Make sure there isn't any empty field"
          });
    }
  else{ connection.query('SELECT * FROM `login`' , (err, rows) => {
        if (err) {
      console.log("Error")
      
        }
        else{
          console.log("success")
         
          for(let i=0;i<rows.length;i++){
           
            if(rows[i].username==req.body.name && rows[i].password==req.body.password  ) {
              
               res.redirect('/map')
            
            }
            else {
              
                 res.sendFile(path.join(__dirname,'msg.html'))
                   }} } }) } 
                    })
                    
worker.js


const zmq = require("zeromq");
const user = require('./data.js')

const sock = new zmq.Pull();
run();
async function run() {

    await sock.connect("tcp://127.0.0.1:3000");
    console.log("Connected to server.")

    for await (const msg of sock) {
        const a=JSON.parse(msg.toString())
        console.log(a)
        const newUser = new user(a)
  newUser.save()
    }
}

data.js


const mongoose = require('mongoose')
const uri= 'mongodb+srv://ervisa:1234@cluster0.e7t7w.mongodb.net/test?retryWrites=true&w=majority'
mongoose.connect(uri,
    {
      useNewUrlParser: true,
      useUnifiedTopology: true, 
      
        
      }).then(()=> console.log('db is connected')).catch()

      const {model, Schema} = require('mongoose')
var schema = new Schema({
   
date:{
    type:String,
    parse:true
},
time:{
    type:String,
    parse:true
},
lat:{
    type:"number",
    parse:true
},

long:{
    type:"number",
    parse:true
},
id:{
    type:"number",
    parse:true
},
int:{
    type:"number",
    parse:true
},
})
module.exports=model('datalist',schema) ;

index.html


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <title>Document</title>
</head>
<body style="background-color:rgb(141, 179, 179);">
  <h1 style="color:darkblue; margin-right: 30%;margin-left: 20%;margin-top: 5%;">WELCOME</h1>

  <form id="form"  action="/submit" enctype="form-data" method="post"ion="/submit"style=" margin-right: 30%;margin-left: 20%;margin-top: 5%;">
    
    <label for="user">Choose Account Type </label>

    <select name="type" id="type">
      <option value="user">User</option>
      <option value="admin">Admin</option>
     
    </select>
   
 <br>
 <br>
    <label  class="form-label" >Username</label>
    <input type="text" class="form-control" name="name"  aria-describedby="emailHelp">
   
  <div class="mb-3">
    <label for="exampleInputPassword1" class="form-label">Password</label>
    <input type="password" class="form-control"  name="password">
  </div>
  <div class="col-lg-8">
    <div class="alert" role="alert" id="message">
        
    </div>
</div>
 <button  id="login">Submit</button></form>
  <br>
  <br>

  
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

</form>

</body>


</html>

msg.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <title>Document</title>
</head>
<body style="background-color:rgb(141, 179, 179);">
  <h2 style="color:darkblue; margin-right: 30%;margin-left: 20%;margin-top: 5%;">The Username or password is not correct!</h2>

  <form id="form" style="margin-right: 30%;margin-left: 30%;margin-top: 2%;">
    
  
  
  <br>
 <a href="/">Back to Login Page</a>
  
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

</form>

</body>


</html>


map.html


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <title>Document</title>
</head>
<body style="background-color:rgb(141, 179, 179);">
  <h3 style="color:darkblue; margin-right: 10%;margin-left: 10%;margin-top: 2%;">WELCOME</h3>

  <form id="form"  action="/submit" enctype="form-data" method="post"ion="/submit"style=" margin-right: 10%;margin-left: 10%;margin-top: 2%;">
    <label for="user">Choose Car Id </label>&emsp;

    <select name="type" id="type">
      <option value="1">1</option>
      <option value="2">2</option>
     
     
    </select>&emsp;
    <label for="user">Choose Time </label>
    &emsp;
    <select name="type" id="type">
      <option value="12:30">12:30</option>
      <option value="13:00">13:00</option>
      <option value="13:30">13:30</option>
      <option value="14:00">14:00</option>
      <option value="14:30">14:30</option>
      <option value="15:00">15:00</option>
      <option value="15:30">15:30</option>
      <option value="16:00">16:00</option>
      <option value="16:30">16:30</option>
      <option value="17:00">17:00</option>
      <option value="17:30">17:30</option>
     
     
    </select> &emsp;
 <button  id="search">Search</button></form>
  <br>
  <br>

  
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

</form>

</body>


</html>