Last active
April 20, 2023 06:23
-
-
Save tusqasi/a5ecb6fc699a43bf951883f9970a8b1f to your computer and use it in GitHub Desktop.
Basic server which can receive files over post request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const multer = require('multer'); | |
const cors = require('cors'); | |
// const sqlite3 = require("sqlite3").verbose(); | |
const axios = require('axios'); | |
const app = express(); | |
app.use(cors()); | |
// app.use(express.static("uploads")); | |
const storage = multer.diskStorage({ | |
destination: (req, file, cb) => { | |
cb(null, 'uploads/'); | |
}, | |
filename: (req, file, cb) => { | |
const unique = Date.now() | |
+ "-" + Math.round(Math.random() * 1e9); | |
const file_name = unique + "-" + file.originalname; | |
cb(null, file_name); | |
}, | |
limits: { fileSize: 50000 } | |
}); | |
const upload = multer({ storage: storage }); | |
app.post('/api/upload_image', upload.single("file"), (req, res) => { | |
console.log("Received File " + req.file.originalname) | |
res.json({ message: 'Files uploaded successfully.' }); | |
res.end() | |
}); | |
app.listen( | |
3000, | |
() => | |
console.log('Server listening on port 3000.') | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment