Wednesday 24 June 2020

Cant modify homepage html and js file once user logs in through the server

Hi everyone, i've been working on a project in which users can upload csv, text files or excel and i show them using chartjs, everything has been fine up until now when i bumped into this issue in which i cant seem to find a way to make changes to the main html and app.js files once i get the login information correctly, my idea was redirecting the user to the updated homepage where the submit file button is allowed once you bypass the login. In case anyone is interested in helping me i will leave my github page https://github.com/Rogerpeke97/Csv-file-converter. Here is a snippet of the server side code: Im using postgresql, expressjs, multer and chartjs as dependencies.​const express = require('express'); const app = express(); const fs = require('fs'); const multer = require('multer'); const path = require('path') const { Pool, Client } = require('pg'); const bodyParser = require("body-parser"); const PORT = 5000; //Set static folder: use is a method that we use when we want to include middleware app.use(express.static(path.join(__dirname, '/'))) /* Way to get file manually instead it´s easier to create a static folder app.get('/', (req, res) => { res.sendFile(path.join(__dirname + '\\index.html')) });*/ app.listen(PORT, () => console.log("Server started on port " + PORT)); //upload file multer let storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, path.join(__dirname, '/')) }, filename: function (req, file, cb) { cb(null, file.originalname) } }) let upload = multer({ storage: storage }) app.post('/uploadedfile', upload.single('myFile'), (req, res, next) => { const file = req.file if (!file) { const error = new Error('Please upload a file') error.httpStatusCode = 400 return next(error) } res.send(file) }) app.use(bodyParser.urlencoded({extended: true})); //getting these values and using them in functiuon userConnect() down below let usernamE = []; let passworD = []; //use function userConnect() to submit data into table //Here i tried to add an html tag to show if login was succesful but it failed //if login was correct it would redirect but im not sure how to change //html contents once i redirect to the home page again app.post('/', async function loginSuccess(req, res){ let b = req.body.myFile; res.redirect("http://localhost:5000") }) app.post('/', async (req, res)=>{ let username = req.body.uname; usernamE.push(username); let password = req.body.psw; passworD.push(password); userConnect(); res.end(); }); //Postgresql const client = new Client({ }) async function userConnect(){ if(usernamE != null){ client.connect(); const text = 'SELECT * FROM users where first_name = $1 and password = $2' //const text = 'insert into users (first_name, password) VALUES($1, $2) RETURNING *' const values = [usernamE.toString(), passworD.toString()] client.query(text, values, (err, res) =>{ //when user doesnt exists postgresql responds with an empty array //thats why i added the length < 3 if(res.rows.length < 3) { loginSuccess(); } else{ console.log(err.stack) } }) } } // callback async ​

Submitted June 24, 2020 at 09:22PM by rogerpeke97

No comments:

Post a Comment