Skip to content

Instantly share code, notes, and snippets.

View xavidram's full-sized avatar

Xavid Ramirez xavidram

View GitHub Profile
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Page Not Found :(</title>
<style>
::-moz-selection {
background: #b3d4fc;
text-shadow: none;
}
module.exports[404] = function pageNotFound(req, res) {
// file path name, render engine will add the .html
const viewFilePath = '404';
// status code of error, 404 being page not found
const statusCode = 404;
// setup a result, not really needed but for future
// multiple status codes, this is useful
const result = {
status: statusCode
};
// Specify any path references here or static path references
staticPaths() {
// Specify that the short cut to src/views is simply a reference to views
this.app.set('views', `${__dirname}/views`);
// Specify to Express that the files we will be serving will be located in...
this.app.use(express.static(path.join(__dirname, 'dist')));
}
// Specify a rendering engine for view templates (html or pug)
addRenderengine() {
// Use the EJS renderFile function to render any HTML files to the view
this.app.use('html', ejs.renderFile);
// tell Express that between HTML and PUG template files, we will use html
this.app.set('view engine', 'html');
}
// Ass REST API nesesary middleware here
addRESTMiddleware() {
// Here we enable CORS in the application and specify a value of 200 to be a
// successful response
this.app.use(cors({ optionSuccessStatus: 200 }));
// We want to specify to express that we want to handle requests in JSON format
this.app.use(json());
// and we also want some help with parsing POST requests as JSON
this.app.use(urlencoded({ extended: true }));
}
/**
* Assign any middleware related to error or information logging
*/
addLoggingMiddleware() {
// Boom handles errors as objects which we can
// use to debug
this.app.use(boom());
// Morgan logs any HTTP request recieved by the server
// whether it is an error or a valid request
this.app.use(morgan('combined')); // combined flag joins all info into one line;
export class Server {
private app: Express; // Declare the Server Express variable Type
private store: any; // MongoStore variable for session logging
constructor() {
// Following our steps
// 2. Create express variable
// 3. assign middleware
// 4. assign static filepaths
import express, { Express } from 'express';
import winston from 'winston';
import boom from 'express-boom';
import morgan from 'morgan';
import cors from 'cors';
import expressValidator from 'express-validator';
// Rather than importing the entire body-parser library, lets just import what we need
import { json, urlencoded } from 'body-parser';
import assert from 'assert';
import session from 'express-session';
{
"rules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"indent": [
true,
"spaces"
/**
* Pre-Hook that targets the Schema's 'save' method.
*/
UserSchema.pre('save', function (next) {
// If the password field is not modified, skip
// this entire pre hook and return to the save method
if (!this.isModified('password')) {
return next();
}