summaryrefslogtreecommitdiffstats
path: root/index.js
blob: 5a310907d1afcb3e28680efcba6461ae52470847 (plain)
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

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var _ = require('lodash');

// create express application
var app = express();
var port = 3000;

// setup REST middleware
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method-Override'));

// CORS support
app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});

// hello
app.use('/hello', function(req, res, next) {
    res.send('Hello World');
    next();
});

// connect to mongodb
mongoose.connect('mongodb://localhost/meanapp');
mongoose.connection.once('open', function() {

    app.models = require('./models/index.js');

    var routes = require('./routes.js');
    _.each(routes, function(controller, route) {
        app.use(route, controller(app, route));
    });

    console.log("listening on port " + port);
    app.listen(port);
});