Wednesday, September 4, 2024

Boilerplate NodeJS Express File Metadata

index.js

var express = require('express');
var cors = require('cors');
require('dotenv').config();

var app = express();

//Import multer for getting file upload
const multer  = require('multer')
const upload = multer({ dest: 'uploads/' })

app.use(cors());
app.use('/public', express.static(process.cwd() + '/public'));

app.get('/', function (req, res) {
    res.sendFile(process.cwd() + '/views/index.html');
});

app.post('/api/fileanalyse', upload.single('upfile'), function (req, res, next) {
  res.json({name: req.file.originalname, type: req.file.mimetype, size: req.file.size});
});

const port = process.env.PORT || 3000;
app.listen(port, function () {
  console.log('Your app is listening on port: ' + port)
});

readme.md

File Metadata Microservice

This is the boilerplate for the File Metadata Microservice project. Instructions for building your project can be found at https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/file-metadata-microservice

sample.env

PORT=3000

package.json

{
  "name": "file_metadata",
  "version": "0.0.1",
  "description": "API project for freeCodeCamp",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.16.4",
    "multer": "^1.4.5-lts.1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/freeCodeCamp/boilerplate-project-filemetadata"
  },
  "keywords": [
    "node",
    "express"
  ],
  "license": "MIT"
}
 

views/index.html

<!DOCTYPE html>

<html>
   <head>
      <title>File Metadata</title>
      <link rel="shortcut icon" href="https://cdn.freecodecamp.org/universal/favicons/favicon-32x32.png" type="image/x-icon"/>
      <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
      <link href="/public/style.css" rel="stylesheet" type="text/css">
   </head>

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

        <h3>Usage:</h3>
        <p>
          Please Upload a File ...
        </p>
        <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>
      </div>
      <div class="footer">
        <p>
          by
          <a href="http://www.freecodecamp.com">freeCodeCamp</a>
        </p>
      </div>
   </body>
</html>

public/style.css

/****** Main Styling ******/

body {
  font-family: 'Roboto', sans-serif;
  font-size: 16px;
  color: #222;
  background-color: #ECF0F1;
  text-align: center;
}

.container {
  padding: 0;
  margin-top: 40px;
}

.footer {
  margin-top: 60px;
}

a {
  color: #2574A9;
}

input {
  display: block;
  position: relative;
  margin: 10px auto;
}

input#button {
  width: 230px;
}

.view {
  position:relative;
  margin: auto;
  margin-top: 40px;
  border: 1px  solid  #aaa;
  padding: 20px;
  width: 60%;
  min-width: 400px;
}
 

uploads/text.txt

This is a test.