Skip to content

Instantly share code, notes, and snippets.

View pablocattaneo's full-sized avatar

Pablo Cattaneo pablocattaneo

View GitHub Profile
// String required
body('name')
.trim()
.not().isEmpty()
.withMessage('This field is required')
.isString()
.withMessage('This field must be an string')
// Email required
@pablocattaneo
pablocattaneo / not_empty_validation.js
Created January 17, 2019 10:15
How to validate that an input is not empty in express using express-validator? #expressValidator
// body('name').not().isEmpty()
// Example
router.post('/insert', [
body('name')
.trim()
.not().isEmpty()
.withMessage('This field is required'),
body('email')
<!-- This simple form uses the novalidate attribute to turn off the browser's automatic validation; -->
<form novalidate>
<p>
<label for="mail">
<span>Please enter an email address:</span>
<input type="email" id="mail" name="mail">
<span class="error" aria-live="polite"></span>
</label>
</p>
<button>Submit</button>
@pablocattaneo
pablocattaneo / solve_CORS.js
Last active January 15, 2019 11:41
How to solve CORS problems in Express? #Express #node #CORS Source: https://www.udemy.com/nodejs-the-complete-guide/learn/v4/t/lecture/12087628?start=15
// In the entry file often index.js or app.js
const express = require('express')
const app = express()
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*') // The second parámeter could be a list of domains
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization')
next()
db.collectionName.explain("executionStats").find()
@pablocattaneo
pablocattaneo / using_object_id_mongodbdriver.js
Last active January 14, 2019 11:18
How to use ObjectId in mongodbdriver? #mongoDb #mongoDbDriver source: https://stackoverflow.com/a/32170615/3599272
const ObjectId = require('mongodb').ObjectId;
const id = req.params.gonderi_id;
const o_id = new ObjectId(id);
@pablocattaneo
pablocattaneo / input_only_numbers.html
Created January 11, 2019 20:59
How to make input only accept numbers between 0 and 10 Avoiding +,-,. and e Source: https://stackoverflow.com/a/32784911/3599272
<input onkeypress="return (event.charCode == 8 || event.charCode == 0 || event.charCode == 13) ? null : event.charCode >= 48 && event.charCode <= 57" min="0" />
@pablocattaneo
pablocattaneo / convert_string_into_number.js
Created January 11, 2019 20:04
How to convert string to number in Javascript? #javascript Source: https://stackoverflow.com/a/1133814/3599272
const x = Number("1000")
const express = require('express')
const router = express.Router()
router.get('/get-users', (req, res) => {
res.status(201).json({doc})
})