nus-mtp/sashimi-note

View on GitHub
sashimi-platform/app.js

Summary

Maintainability
A
0 mins
Test Coverage
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const history = require('connect-history-api-fallback');

const api = require('./routes/api');

const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
app.use(logger('dev'));
app.use(history());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/api', api);

// catch 404 and forward to error handler
const page404Handler = function page404Handler(req, res, next) {
  const err = new Error('Not Found');
  err.status = 404;
  next(err);
};

// error handler
const pageErrorHandler = function pageErrorHandler(err, req, res, next) {
    // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
  res.status(err.status || 500);
  res.render('error');
};

app.use(page404Handler);
app.use(pageErrorHandler);

module.exports = app;