Skip to content

Instantly share code, notes, and snippets.

@canibanoglu
Forked from troygoode/Procfile
Last active November 29, 2019 10:42
Show Gist options
  • Save canibanoglu/21d8cda8165b6adde0057b71628559c1 to your computer and use it in GitHub Desktop.
Save canibanoglu/21d8cda8165b6adde0057b71628559c1 to your computer and use it in GitHub Desktop.
basic example node.js (+express +jade) application configured to run in Heroku(views_layout.jade and views_home.jade should just be "layout.jade" and "home.jade" inside the "views" subdirectory)(public_home.js should just be "home.js" inside the "public" subdirectory)
const jwt = require('jsonwebtoken');
const authService = {
verify: (token) => {
const decodedToken = jwt.verify(token, 'RANDOM_TOKEN_SECRET');
return decodedToken.userId;
},
getUser: (userId) => {
return getUserFromDB(userId);
}
};
module.exports = (req, res, next) => {
try {
const token = req.headers.authorization.split(' ')[1];
const userId = authService.verify(token);
if (req.body.userId && req.body.userId !== userId) {
res.render('home', {sayHelloTo: 'stranger'});
} else {
req.user = authService.getUser(userId);
next();
}
} catch {
res.status(401).json({
error: new Error('Invalid request!')
});
}
};
{
"name": "example",
"version": "0.0.1",
"private": true,
"engines": {
"node": "0.10.x",
"npm": "1.2.x"
},
"dependencies": {
"express": "*",
"jade": "*"
}
}
web: node server.js
$(function(){
alert('Hello world.');
});
var express = require('express');
var auth = require('./authMiddlware');
var app = module.exports = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.use(app.router);
app.use(auth);
app.get('/', function(req, res){
res.render('home', {sayHelloTo: req.user.name});
});
if(!module.parent){
app.listen(process.env.PORT || 3000, function(){
console.log('up and running');
});
}
extends layout
block title
title Overriden Example
block scripts
script(src="http://code.jquery.com/jquery-1.10.0.min.js")
script(src="/home.js")
block content
p Hello #{sayHelloTo}.
doctype 5
html(lang="en")
head
block title
title Example
block stylesheets
body
block body
block scripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment