#expres app.post() headache

1 messages · Page 1 of 1 (latest)

twilit iceBOT
#

@west sequoia

susenkaoreocze Uploaded Some Code

So.... for i dont know how log i have been trying to get this working, but is like my grandmas microwave so it works when it wonts (that means never)

for some context:

// CLIENT SIDE CODE
function addToFileSystem(type) {

    let form = document.querySelector(".dialog-add-form");

    switch (type) {

      . . . some other code

        case "folder":

            let folder;

            for (let input of form.querySelectorAll("input")) {
                if (!input.value) {
                    return;
                }
                if (input.type === "submit") {
                    continue;
                }

                folder = input.value;
            }
            
            fetch(`api/fs/upload-folder`, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({
                    folder,
                    location: JSON.parse(localStorage.getItem('currentFolder'))
                })
            })
            .then(res => res.json())
            .then(res => {
                console.log(res);
            })
            .catch(err => {
                console.log(err);
            })
        break;

        default:
            break;
    }
}

now lets see the server:

const express = require('express')
const sessions = require('express-session')
const formidable = require('express-formidable');
const path = require("path");
const fs = require('fs');
const cors = require('cors');
require('dotenv').config()


var app = express()


// parsing the incoming data
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(formidable());


// setting up sessions
app.use(sessions({
    secret: 'OZA research facility',
    resave: false,
    saveUninitialized: true
}))
var session;

//serving public file
app.use(express.static(path.resolve(__dirname, "./public")));

. . . some other working api routes

function findKeyValue(obj, key) {
    for (let prop in obj) {
        if (prop === key) {
            return obj[prop];
        } else if (typeof obj[prop] === "object") {
            let value = findKeyValue(obj[prop], key);
            if (value !== undefined) {
                return value;
            }
        }
    }
}

// FILE MANAGEMENT SYSTEM
// this endpoint is the problem
app.post('/api/fs/upload-folder', (req, res) => {

        let files = require('./files.json');
        let folder_for_creation = req.body.folder;
        let location_where_to_create = req.body.location;
    
        let folder = {
            "return": location_where_to_create,
            "folders": {},
            "files": []
        }
    
        if (!!location_where_to_create && !!folder_for_creation) {
        
            let location = findKeyValue(files, location_where_to_create);
            location.folders[folder_for_creation] = folder;

            // try {
            //     fs.writeFileSync('./files.json', JSON.stringify(files));
            // } catch (err) {
            //     console.log(err);
            // }

            return res.send({
                status: 200,
                message: "success"
            });
        }
})

client sends request as they usually do... cliks a button -> dev tools show that its pending (forever)...
server does not log anything, even if i add a console.log(req)... complete void
i dont know what the problem is, i tryed googling... found out nothing

if any of you smarter here know how to fix this, i would be really glad for your help :peepoHeart:

Uploaded these files to a Gist
west sequoia
#

it would be more read able if it wouldnt turn it into embed :c

#

oh... i forgot to erase some endpoints ganyuwtf nevermind... nothing serious

west sequoia
#

ok nevermind...
i just replace those POST endpoints with GET endpoints....
nothing i wanted to do but atleast it works .c

west sequoia
#

ah

#

the problem was because of express.json() and formidable() coliding with each other

#

well i moved to bodyParser... silly me couse i forgot about it

#

now it looks like this:

// parse application/json
app.use(bodyParser.json());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

and POST requests are now working :)