File Metadata Microservice -- Do we need a database for this?

I just want to know if we need a database for this challenge. It’s this one. From the looks of it, it doesn’t seem like they want us to create a database, but I just wanted to make sure. Thanks.

there is no need to save the file in database, the solution hsould be very simple in this project

Okay, thanks.

And is this fine for the app.use call for the /public route?

app.use("/public", path.join("express.static(process.cwd())", "/public"));

sorry, im not familiar with this method

Okay, I have another question to ask here.

I tried to do this because I wanted to take a look at the POST request body:

app.post("/api/fileanalyse", upload.single("upfile"), (req, res, next) => {
  console.log(req.body);
});

But this is what I get in the terminal on VS Code:

[Object: null prototype] {}

Here’s my full server.js file:

const express = require("express");
const cors = require("cors");
require("dotenv").config();
const multer = require("multer");
const bodyParser = require("body-parser");

const upload = multer();

const app = express();

app.use(cors());
app.use("/public", express.static(`${process.cwd()}/public`));

app.get("/", (req, res) => {
  res.sendFile(`${process.cwd()}/views/index.html`);
});

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post("/api/fileanalyse", upload.single("upfile"), (req, res, next) => {
  console.log(req.body);
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Your app is listening on port ${port}`);
});

What did I do wrong here and how do I get the full POST request body so I can see what’s in it? I want to start there, after all. Thanks in advance.

Edit: Just to make sure there’s nothing I need to do in the HTML to fix the issue, I’ll also post that here:

<!DOCTYPE html>

<html>
   <head>
      <title>DragonOsman File Metadata</title>
      <link rel="shortcut icon" href="favicon.ico" />
      <link rel="icon" type="image/gif" href="img/animated_favicon1.gif" />
      <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
      <link href="/public/style.css" rel="stylesheet" type="text/css" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   </head>

   <body>
      <main class="container">
        <header>
          <h2>API Project: File Metadata Microservice</h2>

          <h3>Usage:</h3>
          <p>
            Please Upload a File ...
          </p>
        </header>
        <div class="view">
          <h4 id="output"></h4>
          <form enctype="multipart/form-data" method="POST" action="/api/fileanalyse">
            <input id="inputfield" type="file" name="upfile">
            <input id="button" type="submit" value="Upload">
          </form>
        </div>
      </main>
      <footer class="footer">
        <p>
          by 
          <a href="https://www.osmanzakir.dynu.net">DragonOsman</a>
        </p>
      </footer>
   </body>
</html>

try to remove the next parameter from the POST handler function. Idk if its what cause the issue, but the next parameter is used for middleware functions, such is the parsers you use(bodyParser.urlencoded() and multer.single()) and to mount middleware functions you either put them before the handler functions, like you did with upload.single() in your POST method, or you use the use() method, how you did with the bodyParsers. Im not certain, as im also currently undergoing the first back end curriculums, but your POST doesnt seem to be called in the correct manner(app.post(path, middleware, handler), as your handler has the appearance of a middleware(like i explained above), so this might be the cause it doesnt run as intended.

The next method is passed in in the multer.js documentation. And even after take it out, the problem persists.

I hope someone who knows more about this helps me out here.

Hi @DragonOsman
I am not an express expert here but have you considered using
app.use(bodyParser.urlencoded({ extended: true })); instead of the above?

If I understand correctly, extended only tells urlencoded what type req.body can be. false means it can only be either a string or an array, and true means it can be any type.

I managed to solve it. I needed req.file, since if you only upload a file without any textual info req.body is empty and the info you need is in req.file.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.