Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.8 kB
4
Indexable
Never
const express = require('express');
const bodyParser = require('body-parser');
const pdfP =require('pdf2json');
const app  =express();
const crypto= require('crypto');
const multer=require('multer');
const {GridFsStorage} = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const methodOverride = require('method-override');
const mongoose =require('mongoose')
const fs =require('fs')
const getStream =require('get-stream')
const mysql = require('mysql2');
app.use(bodyParser.json());
app.use(bodyParser.raw())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
const path =require('path');
//const db= require("./db");
//const fs= require('fs');
const pdfParse= require("pdf-parse");
const user =require("./models/user");
const uri= 'mongodb+srv://user:dbUser@cluster0.um1b0.mongodb.net/lib?retryWrites=true&w=majority'
const pdfjs=require("pdfjs-dist/legacy/build/pdf")

async function getContent(src){
  const doc = await pdfjs.getDocument(src).promise
  const page =await doc.getPage(2)
  return await page.getTextContent()
}
async function getItems(src){
  const content =await getContent(src)
  const items=content.items
console.log(items[2].str.substring(5,9))
 return items;
}
getItems("sample.pdf");
mongoose.connect(uri,{
  useNewUrlParser: true,
    useUnifiedTopology: true,
 
  
    
  }).then(()=> console.log('db is connected')).catch((err)=> console.error('Error'));
  const a=mongoose.connection;


  var connection = mysql.createPool({
    connectionLimit:10,
    host: 'localhost',
    user: 'root',
    password: '3005',
    database: 'emlak_takip'
  });
  connection.getConnection((err,connection)=>{
    if(err) throw err;
    console.log("connection established")
  });

const upload=multer({dest:"models/files"});





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


    app.get('/register',(req,res)=>{
        res.sendFile(path.join(__dirname,'pass.html'));
    });
    app.get('/user',(req,res)=>{
      res.sendFile(path.join(__dirname,'user.html'));
  });
  app.get('/admin',(req,res)=>{
    res.sendFile(path.join(__dirname,'admin.html'));
});

   app.post("/user",upload.single('file'),(req, res, next) => { 
 
  pdfParse(req.file.path).then(function(data) {
   console.log(data.numpages)
   
})
.catch(function(error){
  console.log(err);
})
getItems(req.file.path)
  });
 


   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{
        user.find({type:req.body.type ,name:req.body.name,pass:req.body.password}).exec()
        .then(user=> {
            if (user.length > 0 && req.body.type=='admin'  ) {
              res.redirect('/admin')
               
              } 
            else if (user.length > 0 && req.body.type=='user'  ) {
             
               res.redirect('/user')
             
             }
            else {
                 
                  res.sendFile(path.join(__dirname,'msg.html'))
                    }})
            .catch(err => {
              console.log(err);
              res.status(500).json({
                error: err
              });}); } });

 app.post("/changepassword", (req, res, next) => {

               if(req.body.oldP==""|| req.body.username==""|| req.body.email==""||req.body.newP==""){
                       return res.status(500).json({
                          message:"Make sure there isn't any empty field"
                         });
                   }
                   else{
                   user.findOneAndUpdate({type:req.body.type,email:req.body.email,name:req.body.username,pass:req.body.oldP},{pass:req.body.newP}).exec()
                       .then(doc=> {if(!doc){
                        return res.status(500).json({
                          message:"The information entered is not correct!",
                       }); 
                       }
                        return res.status(500).json({
                          message:"Password changed successfully!",
                       }); 
                       }).catch(err => {
                             console.log(err);
                             res.status(500).json({
                               error: err
                             });}); } });   
                                      
   app.listen(3000)