Skip to content

Instantly share code, notes, and snippets.

View thihenos's full-sized avatar

Thiago Silva thihenos

View GitHub Profile
let fs = require('fs');
/* No exemplo abaixo, informamos o local que será criado o arquivo
toda a informação que esse arquivo conterá, e por ultimo temos nossa função callback */
fs.writeFile("./files/example.txt",'Um breve texto aqui!', function(err){
//Caro ocorra algum erro
if(err){
return console.log('erro')
}
//Caso não tenha erro, retornaremos a mensagem de sucesso
@thihenos
thihenos / mediumAjaxPostExample.js
Last active August 6, 2018 20:13
Medium Procedure - Example of posting data via AJAX
$(document).ready(function(){
//Exemplo de detecção de um botão, cujo o class é exampleClass
$('.exampleClass').on('click',function(){
//Examplo pegando os dados enviados por parametro
var jsonObjeto = [{teste:$(this).data('teste'), nome:$(this).data('nome')}];
//Examplo pegando os dados diretamente de algum campo
var jsonObjeto = [{teste:$('.classCampo').val(), nome:$('#idCampo').val()}];
//Chamada do Ajax para trazer os detalhes da ordem
$.ajax({
url: 'example/post', //selecionando o endereço que iremos acessar no backend
$(document).ready(function(){
let Itens = [];//Variavel para adicionar os itens retornado do backend
let divItens = document.getElementById("divItens");//Selecionando a div que irá receber o conteúdo gerado automaticamente
//Chamada do Ajax para trazer os detalhes da ordem
$.ajax({
url: '/secured/vagas/', //selecionando o endereço que iremos acessar no backend
type: 'GET', //selecionando o tipo de requesição, PUT,GET,POST,DELETE
sucess: function(){},//Em caso de sucesso
error: function(err){//Em caso de erro
console.log(err);//Exibir o erro no console JS do navegador
@thihenos
thihenos / mediumAjaxExample.html
Last active August 6, 2018 14:13
Medium Procedure - Example of AJAX
<div class="container">
<div id="divMensagemRetorno" class="row" style="display: none;">
<h4 id="mensagemRetorno"></h4>
</div>
<hr class="my-4">
<div id="divItens" class="row"></div>
</div>
@thihenos
thihenos / showAll.html
Created August 5, 2018 23:57
mediumCRUDexampleShow.html
{#materials}
<div id="divMaterial" class="col-lg-4 col-xl-4 col-sm-12 col-md-4">
<h4>{nome}</h4>
<a href="/material/{id}">Material {id}</a>
</div>
{/materials}
let db = require('../models')
exports.new = function(req, res) {
//Example of export function
};
exports.findAll = function(req, res) {
//Example of export function
}
exports.find = function(req, res) {
//Example of export function
'use strict';
module.exports = function (app) {
//getting the file which has all the routes to save any materials
let material = require('./routes/material');
app.get('/material/new',material.new);
app.get('/material',material.findAll);
app.get('/material/:id',material.find);
app.post('/material',material.create);
app.post('/material/:id',material.update);
app.delete('/material/:id',material.destroy);
//Here, I isolate one Route in the file route/application
let application = require('./routes/application');
/* Secured Route
* First, the app will enter in routes/application, so there you can create any validation you want, and use the NEXT function for Node enter in the function that will
* render the page secured
* Following the logic, you can create a lot of routes before it goes to the final rendering HTML
* Example: app.get('/home', route1, route2, route3, routefinal);
*/
app.get('/home',application.IsAuthenticated,function(req,res){
exports.IsAuthenticated = function(req,res,next){
//Passport creates a function to your session called isAuthenticated(), so you can use it to verify if the user really login in the app
console.log(req.isAuthenticated());
if(req.isAuthenticated()){
//So, here you are saying that if the route called had any other function, it will goes to the next one ( which is rendering the HTML )
next();
}else{
//Or else, goes back to login page
res.render('welcome/index',{message:'Ops! This route requires a login!'});
}
// Using Passport for local strategy
passport.use('local-login', new LocalStrategy ({
//In this part, passport will use' the input in HTML, by using it's name for the strategy
usernameField : 'login',
passwordField : 'password'
,passReqToCallback : true},function (req,login,password,done){
//Querying on database to find the user by the email or the login
db.User.find({ where: { username: req.body.login }}).then(function(result) {
//Verify if the query returns a result
if(result){