Skip to content

Instantly share code, notes, and snippets.

@lionello
Last active November 20, 2024 04:29
Show Gist options
  • Save lionello/d1f9d5c198f12590fb70997a93f06edd to your computer and use it in GitHub Desktop.
Save lionello/d1f9d5c198f12590fb70997a93f06edd to your computer and use it in GitHub Desktop.
Minimal Compose app to upload/download a file
services:
service1:
restart: always
build:
context: .
dockerfile: Dockerfile
ports:
- mode: ingress
target: 3000
published: 3000
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/"]
# Use an official Node runtime based on slim as a parent image
FROM node:20-slim
# Set the working directory to /app
WORKDIR /app
# Install curl and any other dependencies
RUN apt-get update \
\
&& apt-get install -y --no-install-recommends \
curl \
&& rm -rf /var/lib/apt/lists/*
# Copy package.json and package-lock.json into the container at /app
COPY package*.json ./
# Install any needed packages specified in package.json
RUN npm install
# Copy the current directory contents into the container
COPY . .
# Make port 3000 available to the world outside this container
EXPOSE 3000
# Run the app when the container launches
ENTRYPOINT [ "node", "main.js" ]
const express = require('express');
const multer = require('multer');
const fs = require('fs');
const path = require('path');
const app = express();
const upload = multer({ dest: 'uploads/' });
app.use(express.static('uploads'));
app.get('/', (req, res) => {
fs.readdir('uploads', (err, files) => {
if (err) {
return res.status(500).send('Unable to scan files!');
}
let fileList = files.map(file => `<li><a href="/${file}">${file}</a></li>`).join('');
res.send(`
<h1>File Upload</h1>
<form ref='uploadForm'
id='uploadForm'
action='/'
method='post'
encType="multipart/form-data">
<input type="file" name="file" />
<input type='submit' value='Upload!' />
</form>
<h2>Uploaded Files</h2>
<ul>${fileList}</ul>
`);
});
});
app.post('/', upload.single('file'), (req, res) => {
res.redirect('/');
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
{
"name": "service1",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "exec node main.js"
},
"dependencies": {
"express": "^4.17.1",
"multer": "^1.4.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment