From 94ceef20eef3cd625917e641d6f76912e130f99f Mon Sep 17 00:00:00 2001 From: Martin Lagrange Date: Tue, 29 Mar 2016 14:43:05 -0700 Subject: [PATCH] Init server --- server/.gitignore | 5 ++ server/app.js | 60 +++++++++++++++++++ server/bin/www | 90 +++++++++++++++++++++++++++++ server/package.json | 17 ++++++ server/public/stylesheets/style.css | 8 +++ server/routes/index.js | 9 +++ server/routes/users.js | 9 +++ server/views/error.ejs | 3 + server/views/index.ejs | 11 ++++ 9 files changed, 212 insertions(+) create mode 100644 server/.gitignore create mode 100644 server/app.js create mode 100755 server/bin/www create mode 100644 server/package.json create mode 100644 server/public/stylesheets/style.css create mode 100644 server/routes/index.js create mode 100644 server/routes/users.js create mode 100644 server/views/error.ejs create mode 100644 server/views/index.ejs diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 0000000..f8c5a21 --- /dev/null +++ b/server/.gitignore @@ -0,0 +1,5 @@ +/node_modules +/dist +/.tmp +/.sass-cache +/bower_components \ No newline at end of file diff --git a/server/app.js b/server/app.js new file mode 100644 index 0000000..03f8595 --- /dev/null +++ b/server/app.js @@ -0,0 +1,60 @@ +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 routes = require('./routes/index'); +var users = require('./routes/users'); + +var app = express(); + +// view engine setup +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'ejs'); + +// 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.use(express.static(path.join(__dirname, 'public'))); + +app.use('/', routes); +app.use('/users', users); + +// 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('error', { + 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('error', { + message: err.message, + error: {} + }); +}); + + +module.exports = app; diff --git a/server/bin/www b/server/bin/www new file mode 100755 index 0000000..b09c4b4 --- /dev/null +++ b/server/bin/www @@ -0,0 +1,90 @@ +#!/usr/bin/env node + +/** + * Module dependencies. + */ + +var app = require('../app'); +var debug = require('debug')('server: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); +} diff --git a/server/package.json b/server/package.json new file mode 100644 index 0000000..e4978e0 --- /dev/null +++ b/server/package.json @@ -0,0 +1,17 @@ +{ + "name": "server", + "version": "0.0.0", + "private": true, + "scripts": { + "start": "node ./bin/www" + }, + "dependencies": { + "body-parser": "~1.13.2", + "cookie-parser": "~1.3.5", + "debug": "~2.2.0", + "ejs": "~2.3.3", + "express": "~4.13.1", + "morgan": "~1.6.1", + "serve-favicon": "~2.3.0" + } +} \ No newline at end of file diff --git a/server/public/stylesheets/style.css b/server/public/stylesheets/style.css new file mode 100644 index 0000000..9453385 --- /dev/null +++ b/server/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +} diff --git a/server/routes/index.js b/server/routes/index.js new file mode 100644 index 0000000..ecca96a --- /dev/null +++ b/server/routes/index.js @@ -0,0 +1,9 @@ +var express = require('express'); +var router = express.Router(); + +/* GET home page. */ +router.get('/', function(req, res, next) { + res.render('index', { title: 'Express' }); +}); + +module.exports = router; diff --git a/server/routes/users.js b/server/routes/users.js new file mode 100644 index 0000000..623e430 --- /dev/null +++ b/server/routes/users.js @@ -0,0 +1,9 @@ +var express = require('express'); +var router = express.Router(); + +/* GET users listing. */ +router.get('/', function(req, res, next) { + res.send('respond with a resource'); +}); + +module.exports = router; diff --git a/server/views/error.ejs b/server/views/error.ejs new file mode 100644 index 0000000..7cf94ed --- /dev/null +++ b/server/views/error.ejs @@ -0,0 +1,3 @@ +

<%= message %>

+

<%= error.status %>

+
<%= error.stack %>
diff --git a/server/views/index.ejs b/server/views/index.ejs new file mode 100644 index 0000000..7b7a1d6 --- /dev/null +++ b/server/views/index.ejs @@ -0,0 +1,11 @@ + + + + <%= title %> + + + +

<%= title %>

+

Welcome to <%= title %>

+ +