Skip to content

Instantly share code, notes, and snippets.

@nastajus
Created March 28, 2018 17:50
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 nastajus/e619ea3a3d1ab1632252efa8f2c0f164 to your computer and use it in GitHub Desktop.
Save nastajus/e619ea3a3d1ab1632252efa8f2c0f164 to your computer and use it in GitHub Desktop.
const express = require('express');
const multer = require('multer');
const ejs = require('ejs');
const path = require('path');
//const bodyParser = require('body-parser');
const port = process.env.PORT || 3004;
const app = express();
const storage = multer.diskStorage({
destination: './public/uploads/',
filename: function(req, file, cb){
cb(null, file.fieldname + Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage: storage });
app.set('view engine', 'ejs');
app.use(express.static('./public'));
//below are attempts to somehow "bind middleware" as I understand it, no difference
//app.use(upload);
//app.use(multer({dest:'./public/uploads/'}).single('file'));
app.get('/', (req, res) => res.render('index'));
app.post('/upload', upload.single('myImage'), (req, res) => {
if(req.err){
res.render('index', {msg: err});
}
else {
console.log(req.file);
res.send('test');
}
});
app.listen(port, function () {
console.log('listening on port ', port);
});
@nastajus
Copy link
Author

this executes successfully in alternate IDE vscode, but my preferred IDE webstorm pukes at both req.err and req.file.

seems like i have several problems rolled into one, that i'm trying to untangle.

  • trying to use webstorm
  • trying to eliminate 'unresolved variable' errors
  • which won't compile with typescript errors, despite running npm install @types/multer @types/express
  • need pointers to understand my gaps with understanding javascript promises, which limits my ability to understand how middleware is supposed to be setup correctly and how to trace for bugs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment