-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!') | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "example", | |
"version": "0.0.1", | |
"private": true, | |
"engines": { | |
"node": "0.10.x", | |
"npm": "1.2.x" | |
}, | |
"dependencies": { | |
"express": "*", | |
"jade": "*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: node server.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
alert('Hello world.'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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