how to upload files to a node js server

Solutions on MaxInterview for how to upload files to a node js server by the best coders in the world

showing results for - "how to upload files to a node js server"
Cameron
05 Feb 2019
1/*
2    This code comes from Vincent Lab
3    And it has a video version linked here: https://www.youtube.com/watch?v=KP_8gN8kh4Y
4*/
5
6// Import dependencies
7const fs = require("fs");
8const YAML = require("js-yaml");
9const express = require("express");
10const multer = require("multer");
11
12// Setup express
13const app = express();
14const port = 3000;
15
16// Setup Storage
17const storage = multer.diskStorage({
18    destination: function (req, file, cb) {
19        // Set the destination where the files should be stored on disk
20        cb(null, "uploads");
21    },
22    filename: function (req, file, cb) {
23        // Set the file name on the file in the uploads folder
24        cb(null, file.fieldname + "-" + Date.now());
25    },
26    fileFilter: function (req, file, cb) {
27
28        if (file.mimetype !== "text/yaml" || file.mimetype !== "text/x-yaml" || file.mimetype !== "application/x-yaml") {
29            // To reject a file pass `false` or pass an error
30            cb(new Error(`Forbidden file type`));
31        } else {
32            // To accept the file pass `true`
33            cb(null, true);
34        }
35
36    }
37});
38
39// Setup multer
40const upload = multer({ storage: storage }); // { destination: "uploads/"}
41
42// Setup the upload route
43app.post("/upload", upload.single("data"), (req, res) => {
44
45    if (req.file) {
46
47        // Get YAML or throw exception on error
48        try {
49
50            // Load the YAML
51            const raw = fs.readFileSync(`uploads/${req.file.filename}`);
52            const data = YAML.safeLoad(raw);
53
54            // Show the YAML
55            console.log(data);
56
57            // Delete the file after we're done using it
58            fs.unlinkSync(`uploads/${req.file.filename}`);
59
60        } catch (ex) {
61
62            // Show error
63            console.log(ex);
64
65            // Send response
66            res.status(500).send({
67                ok: false,
68                error: "Something went wrong on the server"
69            });
70            
71        }
72
73        // Send response
74        res.status(200).send({
75            ok: true,
76            error: "File uploaded"
77        });
78
79    } else {
80
81        // Send response
82        res.status(400).send({
83            ok: false,
84            error: "Please upload a file"
85        });
86
87    }
88
89})
90
91// Start the server
92app.listen(port, () => console.log(`YAML file uploader listening at http://localhost:${port}`));
Theo
21 Jun 2016
1<!--
2    This code comes from Vincent Lab
3    And it has a video version linked here: https://www.youtube.com/watch?v=KP_8gN8kh4Y
4-->
5
6<!DOCTYPE html>
7<html>
8  <head>
9    <title>YAML File Uploader</title>
10  </head>
11  <body>
12    <!-- https://code.tutsplus.com/tutorials/file-upload-with-multer-in-node--cms-32088 -->
13    <form action="http://localhost:3000/upload" enctype="multipart/form-data" method="POST">
14      <input type="file" name="data" />
15      <input type="submit" value="Upload a file" />
16    </form>
17  </body>
18</html>
Sara
09 Oct 2019
1    npm install --save multer
queries leading to this page
node js make file uploadnodejs fileuploadhow to upload files in nodejsnode js upload and read fileupload file to node serverhow to handle upload file in nodejsfile upload nodejs examplenode js upload file to another server code examplenodejs upload file to servernodejs upload filehow uploads work node jsupload files to server using nodejswork with file without uploading to server nodefile upload using node jshow to read upload files nodejshow to upload node js to serverupload file to directory nodejsupload file in node jsupload local file with nodejsnode js upload file fshow to upload files from nodejsnode js how to upload a filehow to upload file on another server nodejsfile uploading in node jsnodejs file uploads uppyupload node js serverupload files in node js httpnode js upload fileupload file to folder nodejsuploading files in nodejs using filepondhow to upload files to a node js serverupload son nodejsnode js file uploaderhow to upload a file to nodejs serverbest ways for file uploading in node jsnode js upload file to folderfile upload node jsupload nodejshow to upload a local file to node js to node jsdo i upload with nodeupload node js to serverupload and retrieve file to server node jsuploading files with nodejsfile uploading with nodejsnodejs upload submitted fileupload with nodejsupload file in folder nodejsupload files using request nodejsupload file nod c3 a9create a file uploading nodejs serverhow to upload file using node jshow to upload nodejs application on serverupload file to different server nodejsnode file uploafdfile uploading in nodejsupload files using request npm nodejsnode upload fileshow to upload file to an api nodejshandle file upload nodejshow to handle file upload in node jsupload files node js expressuploadfile in nodejsform file upload node jshow to upload files to a local nodejs server using javascript and html5file upload in node jsupload file js nodeupload file with nodejsfile upload from url in node jsupload files to node serverhow to handle file upload in node js 3f 2aupload file to server node jsnode file upload examplenodejs simple file upload serverupload a file to nodejs serverhow to upload a file to a nodejs serverhow to upload a file to a server in nodenodejs uploading filesupload file with fsallow upload a file node jsnode js api to upload file into serverupload file nodejsupload file file system nodeuploads file in node jsnodejs upload file to urlnodejs upload to folderhtml upload file node jswhere to upload my nodejs appfile upload and download with nodejshow to file upload 26 download in node jsnode js upload file node jsfile upload and geting back in nodefile upload and download with node jsupload file via url node jsnode js upload local filehow to upload file on server with nodenodejs receive file uploadnodejs file upload libraryupload file response in nodejsfile upload nodejs demo upload file from nodejshow to upload file to server using node jswhere to upload node js project on serverjavascrript upload files to nodejssimple nodejs to handle file upload servernodejs code to upload file servernode js upload filenode file upload from browserfile upload nodejsupload file on server node js upload node js to servernodejs upload file locallynode server function that uploads files to node serveruploading a file on nodejsnode js file uploadupload file node jsdifferent ways of file upload in node jssimple node server for file uploadupload files in node js w3diffrent ways to upload file in nodej jsupload a file node jshow to handle file upload in nodejsuploading files nodejshow to upload files node jsfile upload in node js exampleupload files node jsuploading files node jsfile upload with node js and expressnode file uploadusing fileupload in nodeuploading of files in nodejshow to upload a file in nodejswhat is node js file uploadhow to upload file in nodejsfunction that uploads files to node serverupload files on computer using nodejsnodejs file uploadfile upload in nodjsnode js upload file clientupload file in nodejsupload a file nodejshow to upload files to node app 5dnodejs file uploadingnode file uploadinghow to upload file in node jsfs upload filehow to create a file uploader in node jshow to upload file in project in node jshow to upload files using fs in node jsupload file to nodejsnode js upload file to another server file upload in nodejsnode js code to upload a filefile upload in node js apiupload a file on the server nodejsupload file api nodejsnode js file uploadhow to upload file to node jsnode js upload file from browserjs upload file to the node jsupload type 3d 22file 22 files to nodenodejs uploadfile upload with node jshow to handle file upload in node js 3fhow to upload node js in serverfile upload vanilla nodeupload local file in node jshow to upload file from javascript to noderetrieve file upload nodejsfile upload from nodejs requesthow to use file upload with nodejsupload any file using node jswit fileupload nodejsnodejs module to upload fileupload file html node jsnode js upload file uploadnodejs upload fileshow to save a file upload nodeupload files nodejsnode upload filehow to upload a file in node jshow to file upload in node jsupload file to another server nodejsuploading files in nodejsjs dom upload file to node jsnode js upload filesnode js upload file to serverfile uploader nodejsnode and upload filehow to upload node js project on serverupload a file to a folder nodejsnode file uploadernode js file upload examplenode js uploadnode js upload files to fireupload files in nodej sfile upload in nodejs expressfile upload in nodejsdownload and upload file in nodejshow to upload node js to begetupload a file nodehow to upload file in node jsnode upload file to serversimple file upload server nodenode js receive file uploaduploading files in node jsnode js upload fileshow would you implement a file upload in node js 3fupload and send a file in node jsupload files in node jshow to upload files to a node js server