-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sovattha SOK
committed
Sep 18, 2015
1 parent
a4b9c84
commit 1ceb8a9
Showing
3 changed files
with
172 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,63 @@ | ||
var express = require('express'), | ||
fs = require('fs'), | ||
router = require('./router'), | ||
app = express(); | ||
|
||
// Set path to the views (template) directory | ||
app.set('views', __dirname + '/views'); | ||
|
||
// Enable pretty rendering by Jade | ||
app.locals.pretty = true; | ||
|
||
// Use compress middleware to gzip content | ||
app.use(express.compress()); | ||
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 router = require('./router'); | ||
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.configure('development', function() { | ||
app.use(express.errorHandler({ | ||
dumpExceptions : true, | ||
showStack : true | ||
})); | ||
}); | ||
app.configure('production', function() { | ||
app.use(express.errorHandler()); | ||
}); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
// Routes | ||
app.get('/', router.index); | ||
app.get('/video', router.video); | ||
app.get('/traffic', router.traffic); | ||
|
||
// The number of milliseconds in one day | ||
var oneDay = 86400000; | ||
// Serve up content from public directory | ||
app.use(express.static(__dirname + '/public', { | ||
maxAge : oneDay | ||
})); | ||
app.use(express.directory(__dirname + '/public')); | ||
// Handle 404 | ||
app.use(router._404); | ||
// Handle 500 | ||
app.use(router._500); | ||
// 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('500', { | ||
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('500', { | ||
message: err.message, | ||
error: {} | ||
}); | ||
}); | ||
|
||
app.listen(process.env.PORT || 80); | ||
|
||
exports.listen = function (port) { | ||
app.listen(port); | ||
}; | ||
module.exports = app; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var app = require('../app'); | ||
var debug = require('debug')('portal:server'); | ||
var http = require('http'); | ||
|
||
/** | ||
* Get port from environment and store in Express. | ||
*/ | ||
|
||
var port = normalizePort(process.env.PORT || '80'); | ||
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); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,28 @@ | ||
{ | ||
"name": "soks.fr", | ||
"version": "0.1.0", | ||
"author": "Sovattha SOK <[email protected]>", | ||
"description": "My personal website", | ||
"contributors": [ | ||
{ | ||
"name": "Sovattha SOK", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"dependencies" : { | ||
"express" : "*", | ||
"mocha": "1.3.0", | ||
"jade": "0.32.0", | ||
"mongodb": "*", | ||
"should": "1.0.0" | ||
} | ||
} | ||
"name": "soks.fr", | ||
"version": "0.2.0", | ||
"author": "Sovattha SOK <[email protected]>", | ||
"description": "My personal website", | ||
"scripts": { | ||
"start": "nodemon ./bin/www", | ||
"watch": "grunt watch", | ||
"test": "grunt test" | ||
}, | ||
"contributors": [{ | ||
"name": "Sovattha SOK", | ||
"email": "[email protected]" | ||
}], | ||
"dependencies": { | ||
"express": "~4.13.1", | ||
"mocha": "1.3.0", | ||
"jade": "0.32.0", | ||
"mongodb": "*", | ||
"should": "1.0.0", | ||
"body-parser": "~1.13.2", | ||
"cookie-parser": "~1.3.5", | ||
"debug": "~2.2.0", | ||
"less-middleware": "1.0.x", | ||
"morgan": "~1.6.1", | ||
"serve-favicon": "~2.3.0" | ||
} | ||
} |