Skip to content

Commit f066de4

Browse files
committed
Add nodejs-file-upload sample
1 parent 55658ca commit f066de4

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use an official Node runtime based on slim as a parent image
2+
FROM node:20-slim
3+
4+
# Set the working directory to /app
5+
WORKDIR /app
6+
7+
# Install curl and any other dependencies
8+
RUN apt-get update \
9+
\
10+
&& apt-get install -y --no-install-recommends \
11+
curl \
12+
&& rm -rf /var/lib/apt/lists/*
13+
14+
# Copy package.json and package-lock.json into the container at /app
15+
COPY package*.json ./
16+
17+
# Install any needed packages specified in package.json
18+
RUN npm install
19+
20+
# Copy the current directory contents into the container
21+
COPY . .
22+
23+
# Make port 3000 available to the world outside this container
24+
EXPOSE 3000
25+
26+
# Run the app when the container launches
27+
ENTRYPOINT [ "node", "main.js" ]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
services:
2+
service1:
3+
restart: always
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
ports:
8+
- mode: ingress
9+
target: 3000
10+
published: 3000
11+
healthcheck:
12+
test: ["CMD", "curl", "-f", "http://localhost:3000/"]

samples/nodejs-file-upload/main.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const express = require('express');
2+
const multer = require('multer');
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const app = express();
7+
const upload = multer({ dest: 'uploads/' });
8+
9+
app.use(express.static('uploads'));
10+
11+
app.get('/', (req, res) => {
12+
fs.readdir('uploads', (err, files) => {
13+
if (err) {
14+
return res.status(500).send('Unable to scan files!');
15+
}
16+
let fileList = files.map(file => `<li><a href="/${file}">${file}</a></li>`).join('');
17+
res.send(`
18+
<h1>File Upload</h1>
19+
<form ref='uploadForm'
20+
id='uploadForm'
21+
action='/'
22+
method='post'
23+
encType="multipart/form-data">
24+
<input type="file" name="file" />
25+
<input type='submit' value='Upload!' />
26+
</form>
27+
<h2>Uploaded Files</h2>
28+
<ul>${fileList}</ul>
29+
`);
30+
});
31+
});
32+
33+
app.post('/', upload.single('file'), (req, res) => {
34+
res.redirect('/');
35+
});
36+
37+
app.listen(3000, () => {
38+
console.log('Server started on port 3000');
39+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "service1",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "main.js",
6+
"scripts": {
7+
"start": "exec node main.js"
8+
},
9+
"dependencies": {
10+
"express": "^4.17.1",
11+
"multer": "^1.4.4"
12+
}
13+
}

0 commit comments

Comments
 (0)