Last active
January 7, 2016 21:39
-
-
Save Mozu-CS/99bc301f563aeaf08c4d to your computer and use it in GitHub Desktop.
The app.js file edited to use the /mozu.events route
This file contains hidden or 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 path = require('path'); | |
var favicon = require('serve-favicon'); | |
var logger = require('morgan'); | |
var cookieParser = require('cookie-parser'); | |
var bodyParser = require('body-parser'); | |
var routes = require('./routes/index'); | |
var users = require('./routes/users'); | |
var mozu_events = require('./routes/mozu-events'); | |
var app = express(); | |
// view engine setup | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', 'jade'); | |
// uncomment after placing your favicon in /public | |
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | |
app.use(logger('dev')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use('/', routes); | |
app.use('/users', users); | |
app.use('/mozu.events', mozu_events); | |
// catch 404 and forward to error handler | |
app.use(function(req, res, next) { | |
var err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
// error handlers | |
// development error handler | |
// will print stacktrace | |
if (app.get('env') === 'development') { | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: err | |
}); | |
}); | |
} | |
// production error handler | |
// no stacktraces leaked to user | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: {} | |
}); | |
}); | |
module.exports = app; |
This file contains hidden or 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 router = express.Router(); | |
var apiContext = require('mozu-node-sdk/clients/platform/application')(); | |
var isRequestValid = require('mozu-node-sdk/security/is-request-valid'); | |
/* GET /mozu.events */ | |
router.get('/', function(req, res, next) { | |
res.render('events', { title: 'Eventing' }); | |
}); | |
router.use(function(req, res, next) { | |
isRequestValid(apiContext.context, req, function(err) { | |
if(err) { | |
res.status(401); | |
res.render('error', {message: err.message, error: err}); | |
} else { | |
console.log("Validated request."); | |
req.mozu = {isValid: true}; | |
next(); | |
} | |
}); | |
}); | |
/* POST against /mozu.events */ | |
router.post('/', function(req, res, next) { | |
console.log("Received POST request..."); | |
console.log(req.headers); | |
console.log(req.body); | |
console.log("Is Valid? " + (req.mozu.isValid ? "\u2713" : "x")); | |
res.sendStatus(200); | |
}); | |
module.exports = router; |
This file contains hidden or 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
{ | |
"appKey": "Grobelny.ExpressJSEvents.1.0.0.Release", | |
"sharedSecret": "q1w2e3r4t5y6uiopasdfghjklz6x5c4v3b2n1m", | |
"baseUrl": "https://home.mozu.com/", | |
"tenant": "12345", | |
} |
This file contains hidden or 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
router.use(function(req, res, next) { | |
isRequestValid(apiContext.context, req, function(err) { | |
if(err) { | |
res.status(401); | |
res.render('error', {message: err.message, error: err}); | |
} else { | |
console.log("Validated request."); | |
next(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment