Skip to content

Instantly share code, notes, and snippets.

View thihenos's full-sized avatar

Thiago Silva thihenos

View GitHub Profile
var fs = require('fs');
//Convertendo binario em arquivo
function base64_decode(base64str,fileName){
var bitmap = new Buffer (base64str, 'base64');
fs.writeFileSync('src/temp/'+fileName+'',bitmap, 'binary', function (err){
if(err){
console.log('Conversao com erro');
}
@thihenos
thihenos / convertFileToBitSave.js
Last active June 30, 2018 20:23
Example for medium procedure
/*
* Example with one file upload
* In this example, inside single() function, we use the name of the name of the element in the html
* which we are sending the post, in this case, it is called file ( check line 19 of file.dust file )
*/
app.post('/file', upload.single('file'),function(req, res) {
//res.json(req.file);
if(req.file){
//Reading the file saved in src/temp folder
let fileContent = base64_encode(req.file.filename);//sendind filename which Multer gives
<!-- This is assuming that this document will get in folder Layout, the dust file layout as it template -->
{>"layout/layout" /}
<!-- This variable, is responsable for get all HTML content and compile it together with the {+body /} in layout file-->
{<body}
<div class="row"><br></div>
<main role="main" class="container">
<div class="jumbotron">
<h1><p><strong>Here it is my second dust file!</strong></p></h1>
/*
* Sync all tables to database
*/
db.sequelize.sync({force:true}).then(function(){
console.log('DB Connection - OK');
let crypto = require('crypto');
//Lib for crypting strings
let crypto = require('crypto');
//Creating my cipher, by using the key REPLACE_YOUR_KEY_HERE
// 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){
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!'});
}
//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){
'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);
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
@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}