|
| 1 | +// Copyright 2015, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +var express = require('express'); |
| 17 | +var path = require('path'); |
| 18 | +var logger = require('morgan'); |
| 19 | +var cookieParser = require('cookie-parser'); |
| 20 | +var bodyParser = require('body-parser'); |
| 21 | + |
| 22 | +var routes = require('./routes/index'); |
| 23 | +var hello = require('./routes/hello'); |
| 24 | + |
| 25 | +var app = express(); |
| 26 | + |
| 27 | +// view engine setup |
| 28 | +app.set('views', path.join(__dirname, 'views')); |
| 29 | +app.set('view engine', 'jade'); |
| 30 | + |
| 31 | +app.use(logger('dev')); |
| 32 | +app.use(bodyParser.json()); |
| 33 | +app.use(bodyParser.urlencoded({ extended: false })); |
| 34 | +app.use(cookieParser()); |
| 35 | +app.use(express.static(path.join(__dirname, 'public'))); |
| 36 | + |
| 37 | +app.use('/', routes); |
| 38 | +app.use('/hello', hello); |
| 39 | + |
| 40 | +// catch 404 and forward to error handler |
| 41 | +app.use(function(req, res, next) { |
| 42 | + var err = new Error('Not Found'); |
| 43 | + err.status = 404; |
| 44 | + next(err); |
| 45 | +}); |
| 46 | + |
| 47 | +// error handlers |
| 48 | + |
| 49 | +// development error handler |
| 50 | +// will print stacktrace |
| 51 | +if (app.get('env') === 'development') { |
| 52 | + app.use(function(err, req, res) { |
| 53 | + res.status(err.status || 500); |
| 54 | + res.render('error', { |
| 55 | + message: err.message, |
| 56 | + error: err |
| 57 | + }); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +// production error handler |
| 62 | +// no stacktraces leaked to user |
| 63 | +app.use(function(err, req, res) { |
| 64 | + res.status(err.status || 500); |
| 65 | + res.render('error', { |
| 66 | + message: err.message, |
| 67 | + error: {} |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | + |
| 72 | +module.exports = app; |
0 commit comments