Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/28164d528d08d57806b4f6530da42459 to your computer and use it in GitHub Desktop.
Save code-boxx/28164d528d08d57806b4f6530da42459 to your computer and use it in GitHub Desktop.
NodeJS Express File Upload

NODEJS EXPRESS FILE UPLOAD

https://code-boxx.com/upload-files-nodejs-express/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Create an uploads folder.
    • Install required modules - npm i express express-fileupload
    • Start the Express server - node 1-server.js.
  2. Access http://localhost/ in your browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (A) INITIALIZE
// (A1) LOAD REQUIRED MODULES
const path = require("path"),
express = require("express"),
fileUpload = require("express-fileupload");
// (A2) EXPRESS + MIDDLEWARE
// https://www.npmjs.com/package/express-fileupload
const app = express();
app.use(fileUpload());
// (B) EXPRESS HTTP
// (B1) UPLOAD PAGE
app.get("/", (req, res) => res.sendFile(path.join(__dirname, "/2-upload.html")));
// (B3) MANAGE UPLOAD
app.post("/upload", (req, res) => {
// (B3-1) UPLOADED FILE & DESTINATION
let upfile = req.files.upfile,
updest = __dirname + "/uploads/" + upfile.name;
// (B3-2) MOVE UPLOADED FILE
upfile.mv(updest, err => {
if (err) { return res.status(500).send(err); }
res.send("File uploaded!");
});
});
// (C) GO!
app.listen(80);
<!DOCTYPE html>
<html>
<head>
<title>Simple Upload Form</title>
</head>
<body>
<form action="http://localhost/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="upfile" required>
<input type="submit" value="Upload!">
</form>
</body>
</html>
md uploads
call npm i express express-fileupload
node 1-server.js
mkdir -m 777 uploads
source "npm i express express-fileupload"
node ./1-server.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment