Skip to content

Commit 7491ced

Browse files
committed
Added project files from How to Upload Files to a Node.js Server
1 parent db4261d commit 7491ced

4 files changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Import dependencies
2+
const fs = require("fs");
3+
const YAML = require("js-yaml");
4+
const express = require("express");
5+
const multer = require("multer");
6+
7+
// Setup express
8+
const app = express();
9+
const port = 3000;
10+
11+
// Setup Storage
12+
const storage = multer.diskStorage({
13+
destination: function (req, file, cb) {
14+
// Set the destination where the files should be stored on disk
15+
cb(null, "uploads");
16+
},
17+
filename: function (req, file, cb) {
18+
// Set the file name on the file in the uploads folder
19+
cb(null, file.fieldname + "-" + Date.now());
20+
},
21+
fileFilter: function (req, file, cb) {
22+
23+
if (file.mimetype !== "text/yaml" || file.mimetype !== "text/x-yaml" || file.mimetype !== "application/x-yaml") {
24+
// To reject a file pass `false` or pass an error
25+
cb(new Error(`Forbidden file type`));
26+
} else {
27+
// To accept the file pass `true`
28+
cb(null, true);
29+
}
30+
31+
}
32+
});
33+
34+
// Setup multer
35+
const upload = multer({ storage: storage }); // { destination: "uploads/"}
36+
37+
// Setup the upload route
38+
app.post("/upload", upload.single("data"), (req, res) => {
39+
40+
if (req.file) {
41+
42+
// Get YAML or throw exception on error
43+
try {
44+
45+
// Load the YAML
46+
const raw = fs.readFileSync(`uploads/${req.file.filename}`);
47+
const data = YAML.safeLoad(raw);
48+
49+
// Show the YAML
50+
console.log(data);
51+
52+
// Delete the file after we're done using it
53+
fs.unlinkSync(`uploads/${req.file.filename}`);
54+
55+
} catch (ex) {
56+
57+
// Show error
58+
console.log(ex);
59+
60+
// Send response
61+
res.status(500).send({
62+
ok: false,
63+
error: "Something went wrong on the server"
64+
});
65+
66+
}
67+
68+
// Send response
69+
res.status(200).send({
70+
ok: true,
71+
error: "File uploaded"
72+
});
73+
74+
} else {
75+
76+
// Send response
77+
res.status(400).send({
78+
ok: false,
79+
error: "Please upload a file"
80+
});
81+
82+
}
83+
84+
})
85+
86+
// Start the server
87+
app.listen(port, () => console.log(`YAML file uploader listening at http://localhost:${port}`));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "back-end",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "MIT",
12+
"dependencies": {
13+
"express": "^4.17.1",
14+
"js-yaml": "^3.13.1",
15+
"multer": "^1.4.2"
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
date: 2019-08-07T00:00:00.000Z
2+
customer:
3+
first_name: Dorothy
4+
family_name: Gale
5+
items:
6+
- name: Water Bottle
7+
description: Water Bottle (Filled)
8+
price: 1.47
9+
quantity: 4
10+
- name: Knife and fork set
11+
description: Knife and fork set in stainless steel
12+
price: 133.7
13+
quantity: 1
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>YAML File Uploader</title>
5+
</head>
6+
<body>
7+
<!-- https://code.tutsplus.com/tutorials/file-upload-with-multer-in-node--cms-32088 -->
8+
<form action="http://localhost:3000/upload" enctype="multipart/form-data" method="POST">
9+
<input type="file" name="data" />
10+
<input type="submit" value="Upload a file" />
11+
</form>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)