Upload with express-validator
See original GitHub issueI have a simple blog post upload form with post_name, post_content & post_image. How & where can I start using check validator function? Please help.
Here is my add-post.ejs code
<% include partials/header %>
<div class="body-content container">
<div class="col-md-9 col-xs-12">
<form method="post" enctype="multipart/form-data">
<fieldset class="form-group row">
<legend class="col-form-legend col-sm-1-12">Add New Post</legend>
<% if( errors ) { %>
<p class="text-danger">Please fix the following errors.</p>
<ul>
<% errors.forEach( function( error ) { %>
<li class="text-danger">
<%= error.msg %>
</li>
<% }) %>
</ul>
<% } %>
<div class="col-sm-1-12">
<div class="form-group">
<label class="col-sm-1-12 col-form-label">Post Title</label>
<input type="text" class="form-control" name="post_title" placeholder="">
</div>
<div class="form-group">
<label>Post Content</label>
<textarea type="text" name="post_content" rows="5" class="form-control" placeholder=""></textarea>
</div>
<div class="form-group">
<label>Featured Image</label>
<input type="file" name="post_image" id="post_image" accept="image/*">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit Post</button>
</div>
</div>
</fieldset>
</form>
</div>
<% include partials/sidebar %>
</div>
<% include partials/footer %>
My app.js code:
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var multer = require('multer');
//var expressValidator = require('express-validator');
const { check, validationResult } = require('express-validator/check');
const { matchedData, sanitize } = require('express-validator/filter');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, 'uploads/')
},
filename: function (req, file, callback) {
console.log(file)
callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
})
var app = express();
// Set view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// Set static path
app.use(express.static(path.join(__dirname, 'public')));
// Global vars
app.use(function (req, res, next) {
res.locals.errors = null;
res.locals.rep = null;
next();
});
// Homepage
app.get('/', function (req, res) {
res.render('index', {
title: 'Homepage'
});
});
// Render Add New Post
app.get('/add-post', function (req, res) {
res.render('add-post', {
title: 'Add New Post'
});
});
// Post New Post
app.post('/add-post', (req, res, next) => {
// Image upload
var upload = multer({
storage: storage,
fileFilter: function (req, file, callback) {
var ext = path.extname(file.originalname)
if (ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
return callback(res.end('Only images are allowed'), null)
}
callback(null, true)
}
}).single('post_image');
upload(req, res, function (err) {
if( err ) {
res.end(err);
} else {
console.log(req.body);
}
var re1 = check('post_title').isEmpty().withMessage('Post title is required');
// Get the validation result whenever you want; see the Validation Result API for all options!
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.render('add-post', {
title: 'Add New Post',
errors: errors.array(),
rep: re1
});
} else {
return res.render('add-post', {
title: 'Add New Post',
rep: re1
});
}
});
});
// Start server
app.listen(3000, function (req, res) {
console.log('Server started at port 3000...');
});
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
How to validate file input with express-validator?
In order to validate a file input field you could use multer: middleware that adds a body object and a file ...
Read more >how to use express-validator for validation of input file? #276
I am using express-fileupload to parse the uploaded files, and you can access the filename as req.files.myfile.name, where myfile is the ...
Read more >Getting Started · express-validator
express -validator is a set of express.js middlewares that wraps validator.js validator and sanitizer functions. Installation. Install it using npm (make sure ...
Read more >Form Data Validation in Node.js with express-validator
Standard Validation Rules with express-validator · isEmail() : This validator function checks if the incoming string is a valid email address.
Read more >How to validate uploaded files in Node JS - DEV Community
In this note, we'll look at how we can handle file validation and compression in Node JS. If you have a better way...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Store the files somewhere temporarily, verify the body, and then either trash the file or move it to permanent storage…
There are no words to describe how stupid I feel right now. Such a dumb mistake. Thank you very much!