From 28d94e2b17492ecb37a0c2565472b75e8a875c72 Mon Sep 17 00:00:00 2001 From: Jan Kuri Date: Sat, 3 Sep 2016 00:57:51 +0200 Subject: [PATCH] docs: express backed example --- README.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 02f6a151..3b2766bc 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ npm install ng2-uploader #### Backend Examples 1. [NodeJS using HapiJS](https://github.com/jkuri/ng2-uploader#backend-example-using-hapijs) -2. [PHP (Plain)](https://github.com/jkuri/ng2-uploader#backend-example-using-plain-php) +2. [NodeJS using express](https://github.com/jkuri/ng2-uploader#backend-example-using-express) +3. [PHP (Plain)](https://github.com/jkuri/ng2-uploader#backend-example-using-plain-php) ### Basic Example @@ -396,6 +397,42 @@ server.start(() => { }); ```` +### Backed example using express + +````js +const express = require('express'); +const cors = require('cors'); +const multer = require('multer'); +const path = require('path'); + +const app = express(); +app.use(cors()); + +const upload = multer({ + dest: 'uploads/', + storage: multer.diskStorage({ + filename: (req, file, cb) => { + let ext = path.extname(file.originalname); + cb(null, `${Math.random().toString(36).substring(7)}${ext}`); + } + }) +}); + +app.post('/upload', upload.any(), (req, res) => { + res.json(req.files.map(file => { + let ext = path.extname(file.originalname); + return { + originalName: file.originalname, + filename: file.filename + } + })); +}); + +app.listen(10050, () => { + console.log('ng2-uploader server running on port 10050.'); +}); +```` + ### Backend example using plain PHP ````php