๐Ÿ“ฆ nilgradisnik / backbone-scaffolding

๐Ÿ“„ server.js ยท 123 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123/**
 * Node.js server for web app testing (http://nodejs.org)
 * This is only to get you started working on your web client easily 
 *
 * @author Nil Gradisnik <nil.gradisnik@gmail.com>
 */

/**
 * Module imports
 * Install modules using npm package manager (https://github.com/isaacs/npm)
 */
var express = require('express');
var stylus  = require('stylus');
var nib     = require('nib');
var hbs     = require('hbs');

/**
 * Express web app
 */
var app = module.exports = express();

/**
 * Express configuration
 */
app.configure(function() {

  // Setup Handlebars templating engine
  // More info: (https://github.com/donpark/hbs)
  app.set('view engine', 'html');
  app.engine('html', hbs.__express);

  app.use(app.router);

  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

/**
 * DEVELOPMENT
 */
app.configure('development', function() {

  var DEV_PATH = '/../public';

  // stylus css compiler middleware
  app.use(stylus.middleware({
    src: __dirname+DEV_PATH+'/style',
    dest: __dirname + DEV_PATH,
    compile: function(str, path) {
      return stylus(str)
        .set('filename', path)
        .set('compress', false)
        .use(nib());
    }
  }));

  // Tell Handlebars to look for templates here
  app.set('views', __dirname + DEV_PATH);

  app.use(express.static(__dirname + DEV_PATH));
});

/**
 * PRODUCTION
 * Run nodejs with NODE_ENV=production
 */
app.configure('production', function() {

  var PROD_PATH = '/../public/build/output';

  app.use({ src: __dirname + PROD_PATH });

  app.set('views', __dirname + PROD_PATH);

  app.use(express.static(__dirname + PROD_PATH));
});

// Dummy user user data
var User = {
  userId : "12345",
  name : "John Doe",
  email : "john@gmail.com"
};

/**
 * Routes
 */
app.get('/', function(req, res) {

  // Send fake user data to index.html
  res.locals = {
    user: User
  };
  res.render('index');
});
app.get('/users', function(req, res) {
  res.send('ok');
});
app.post('/users', function(req, res) {
  res.send('ok');
});

// Handlebars helper, returns json string
hbs.registerHelper('json', function(context) {
  return JSON.stringify(context);
});

/**
 * Start server
 */
var PORT = 1337;
app.listen(PORT);
console.log("backbone-scaffolding server listening on http://localhost:"+PORT+" ["+app.settings.env+"]");

/**
 * Generic error handling
 */
process.on('uncaughtException', function(err) {
  console.error("FATAL ERROR: "+err.message);
  console.error('Stack: '+err.stack);
  console.error("Shuting down app...");
  process.exit(1);
});