Browse Source

add logs

pull/29/head
Cian Butler 5 years ago
parent
commit
dd2cf7cbca
8 changed files with 148 additions and 34 deletions
  1. +36
    -16
      app.js
  2. +90
    -0
      bin/www
  3. +8
    -2
      package.json
  4. +0
    -5
      views/404.jade
  5. +0
    -5
      views/500.jade
  6. +3
    -0
      views/error.ejs
  7. +0
    -6
      views/error.jade
  8. +11
    -0
      views/index.ejs

server.js → app.js View File

@@ -1,6 +1,10 @@
var express = require('express'),
nodemailer = require("nodemailer"),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser');
config = require("./config.json"),
app = express(),
smtpTransport = nodemailer.createTransport(config.email),
@@ -8,8 +12,15 @@ baseUrl = '/api/';

// Serve Static files generate from hexo
// TODO Freshly generate files with hexo on start
app.use(express.static('public'));
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// dynamically create contact page
app.get('/about/contact/*',function(req,res) {
@@ -49,10 +60,8 @@ app.get(baseUrl + 'contact', function(req, res) {
}
smtpTransport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.end("error");
} else {
console.log("Message sent: " + info.response);
res.end("sent");
}
});
@@ -60,19 +69,30 @@ app.get(baseUrl + 'contact', function(req, res) {
});
});

// Handle 404
app.use( function(req, res) {
res.status(404).sendFile(__dirname + '/public/404.html');
console.log('404', req);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
res.status(err.status).sendFile(path.join(__dirname + '/public/404.html'));
});

// Handle 500
app.use( function(error, req, res, next) {
res.status(500);
res.render('500.jade', {title:'500: Internal Server Error', error: error});
console.log('500', req);
});
// 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
});
});
}

app.listen(3000, function() {
console.log("Express Started on Port 3000");
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500).sendFile(path.join(__dirname + '/public/404.html'));
});

module.exports = app;

+ 90
- 0
bin/www View File

@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('express:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

+ 8
- 2
package.json View File

@@ -12,6 +12,8 @@
},
"license": "MIT",
"scripts": {
"start": "hexo generate && node ./bin/www",
"test": "echo \"Error: no test specified\" && exit 1",
"setup": "git clone https://github.com/redbrick/hexo-theme-icarus.git themes/redbrick-theme",
"postinstall": "cd themes/redbrick-theme && bower install"
},
@@ -19,7 +21,11 @@
"version": "3.2.2"
},
"dependencies": {
"express": "^4.14.0",
"body-parser": "~1.15.1",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"ejs": "~2.4.1",
"express": "~4.13.4",
"hexo": "^3.2.2",
"hexo-cli": "latest",
"hexo-cli-extras": "^1.0.3",
@@ -38,7 +44,7 @@
"hexo-renderer-marked": "^0.2.10",
"hexo-renderer-stylus": "^0.3.1",
"hexo-server": "^0.2.0",
"jade": "^1.11.0",
"morgan": "~1.7.0",
"nodemailer": "^2.5.0",
"serve-favicon": "^2.3.0"
}


+ 0
- 5
views/404.jade View File

@@ -1,5 +0,0 @@
extends error
block content
h1= title
p Dude, where is my page?
a(href='/') Take me to the homepage!

+ 0
- 5
views/500.jade View File

@@ -1,5 +0,0 @@
extends error
block content
h1= title
p #{title}
p #{error}

+ 3
- 0
views/error.ejs View File

@@ -0,0 +1,3 @@
<h1><%= message %></h1>
<h2><%= error.status %></h2>
<pre><%= error.stack %></pre>

+ 0
- 6
views/error.jade View File

@@ -1,6 +0,0 @@
html
head
title Error
body
h1 An error occurred!
block content

+ 11
- 0
views/index.ejs View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
</body>
</html>

Loading…
Cancel
Save