<file_sep>var mongoose = require('mongoose');

// Comment Schema
var CommentSchema = mongoose.Schema({
	autor: String,
	comentario: String,
	data: String,
	dataCompleta: String,
	email: String,
	site: String,
	outros: String,
})

module.exports = CommentSchema;<file_sep># Blog template
Template de blog usando roteamento de views do express.<file_sep>let router = require('express').Router();

router.get('/:calc/:x/:y', (req, res) => {
	console.log(req.params)

	var z = {};

	// console.log('x int: ', req.params.x.split('.')[0].length)
	// console.log('x dot: ', req.params.x.split('.')[1].length)
	// console.log('y int: ', req.params.y.split('.')[0].length)
	// console.log('y dot: ', req.params.y.split('.')[1].length)

	// console.log(req.params.x.split('.')[0].length / req.params.x.split('.')[1].length)
	// console.log(req.params.y.split('.')[0].length / req.params.y.split('.')[1].length)

	if (req.params.x.length > 16 ||
		req.params.y.length > 16)
		z.err = 'Params given are too high, the result is not going to be too accurate'

	var x = parseFloat(req.params.x)
	var y = parseFloat(req.params.y)
	var calc = req.params.calc

	console.log(x)
	console.log(y)

	if (isNaN(x) || isNaN(y)) {
		return res.send({
			err: 'Params given are not number type'
		})
	}

	switch (calc) {
		case '+':
		case 'soma':
		case 'plus':
		case 'add':
			z.result = x + y;
			break;
		case '-':
		case 'sub':
		case 'minus':
		case 'less':
			z.result = x - y;
			break;
		case '*':
		case 'multi':
		case 'mult':
		case 'times':
			z.result = x * y;
			break;
		case ':':
		case 'div':
		case 'take':
		case 'split':
			z.result = x / y;
			break;
		case '√':
		case 'root':
		case 'raiz':
			z.result = Math.pow(x, 1 / y);
			break;
		case '^':
		case 'exp':
			z.result = Math.pow(x, y);
			break;
		default:
			z.err = 'Operation \'' + calc + '\' not known';
	}
	console.log
	res.send(z)
})


module.exports = router;<file_sep>$(document).ready(function () {
	// Init Modal
	$('.modal').modal();
	$("select[required]").css({
		display: "inline",
		height: 0,
		padding: 0,
		width: 0
	});
	$('.button-collapse').sideNav();
	
});<file_sep>let path = require('path');
let express = require('express');
let cors = require('cors')
let bodyParser = require('body-parser');
let mongoose = require('mongoose');
let database = require('./config/database').database
let app = express();

mongoose.Promise = require('bluebird');
mongoose.connect(database, {
	useMongoClient: true
}, (err) => {
	if (err) return console.log("NÃO FOI POSSIVEL CONECTAR COM O MLAB\n" + err);
	console.log("CONEXÃO COM O MLAB EFETUADA\nPORTA 3000");
});
var db = mongoose.connection;

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
	extended: false
}));
// parse application/json
app.use(bodyParser.json());

app.use(cors({
	origin: "*",
	methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
	preflightContinue: false,
	optionsSuccessStatus: 204,
	allowedHeaders: ['Origin', 'X-Requested-With', 'xx-access-token', 'Content-Type', 'Accept']
}))

// Load view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use("/", express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
	res.redirect('/articles');
})

var soma = require('./routes/soma');
app.use('/soma', soma);
// Router dos Artigos
var articles = require('./routes/articles');
app.use('/articles', articles);
// Router dos Admins
var admin = require('./routes/admins');
app.use('/admin', admin);
var articlesAPI = require('./routes/api/articles');
app.use('/api/articles', articlesAPI); 
var calc = require('./routes/api/calc');
app.use('/api/calc', calc);

app.listen(process.env.PORT || 3000, () => {
	var x = {};
	x.port = process.env.PORT || 3000;
	console.log('running', x.port)
})<file_sep>let router = require('express').Router();
let bcrypt = require('bcrypt');

let Admin = require('../models/admin');
let titulo = require('../config/titulo');

router.post('/', (req, res) => {
	console.log('cadastrando...')
	bcrypt.hash(req.body.senha, 5, (err, hash) => {
		if (err) res.send({
			err
		});

		var admin = new Admin({
			username: req.body.username,
			senha: hash
		})

		admin.save(admin, (err) => {
			if (err) res.send({
				err
			});
			console.log('cadastrado !!!')
			res.send(admin);
		})
	})
})

router.post('/login/:view', (req, res) => {
	Admin.findOne({
		username: req.body.username
	}, (err, admin) => {
		if (err) return res.send({
			err
		})
		if (!admin) return res.redirect('/')
		bcrypt.compare(req.body.senha, admin.senha, (err, isMatch) => {
			if (err) res.send({err});
			if (isMatch) res.render(req.params.view, {
				pode: true,
				titulo: titulo + 'Adicionar post'
			});
			else res.redirect('/')
		});
	});
});

module.exports = router;<file_sep>let router = require('express').Router();

let Article = require('../models/article');
let titulo = require('../config/titulo');

router.get('/', (req, res) => {
	Article.find({}).sort({
		_id: -1
	}).exec((err, artigos) => {
		if (err) res.send({
			err
		});

		res.render('blog', {
			artigos,
			titulo: titulo + "posts"
		});
	})
})

router.get('/:id', (req, res) => {
	Article.findById(req.params.id, (err, artigo) => {
		if (err) res.send({
			err
		});
		res.render('artigo', {
			artigo,
			titulo: titulo + artigo.nome
		});
	});
});

router.get('/tag/:tag', (req, res) => {
	var tag = req.params.tag;
	Article.find({
		tags: tag
	}).sort({
		_id: -1
	}).exec((err, artigos) => {
		if (err) res.send({
			err
		});
		res.render('resultado', {
			artigos,
			tag,
			titulo: titulo + tag,
		});
	});
});

router.post('/', (req, res) => {
	var d = new Date();
	var article = new Article({
		nome: req.body.nome,
		dataCompleta: d.toString(),
		data: getDateNow(d),
		corpo: req.body.corpo,
		tags: getTags(req.body.tags),
	});

	article.save(article, (err) => {
		if (err) res.send({
			err
		})
		res.redirect('/articles');
	})
})

router.put('/:id', (req, res) => {
	console.log('putando')
	if (req.body.tags) var article = new Article({
		_id: req.params.id,
		nome: req.body.nome,
		corpo: req.body.corpo,
		tags: getTags(req.body.tags),
	})
	else var article = new Article({
		_id: req.params.id,
		nome: req.body.nome,
		corpo: req.body.corpo,
	})
	article.update(article, (err) => {
		console.log('updateando')

		if (err) res.send({
			err
		})
		res.redirect('/articles');
	});
})

router.post('/comment/:post', (req, res) => {
	var d = new Date();
	Article.findById(req.params.post, (err, postBD) => {
		if (err) res.send({
			err
		});
		var comment = {
			autor: req.body.autor,
			comentario: req.body.comentario,
			email: req.body.email,
			site: req.body.site,
			outros: req.body.outros,
			dataCompleta: d.toString(),
			data: getDateNow(d)
		}
		postBD.comentarios.push(comment)
		let post = new Article(postBD)
		post.save(post, (err) => {
			console.log(postBD._id)
			var url = '/articles/' + postBD._id.toString();
			res.redirect(url)
		})
	})
})

function getDateNow(d) {
	var aux = d.toString().split(' ');
	var str = '';

	for (var i = 1; i < 4; i++)
		str += (i == 3) ? aux[i] : aux[i] + ' ';

	return str;
}

function getTags(str) {
	var aux = [];
	str.split(',').map((tag) => {
		aux.push(tag.trim().toLowerCase())
	})

	aux = aux.filter(function (item, pos) {
		return aux.indexOf(item) == pos;
	})

	return aux;
}

router.get('/test/', (req, res) => {
	res.send(teste())
})

function teste() {
	Article.findById('5a34a6d3a001a60014c34d23').exec((err, res) => {
		if (err) return {err, res};
		return res
	})
}

module.exports = router;<file_sep>let router = require('express').Router();

router.get('/:x/:y', (req, res) => {
    res.render('soma', {
        soma: parseInt(req.params.x) + parseInt(req.params.y)
    });
})

module.exports = router;