-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (28 loc) · 1.59 KB
/
server.js
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
var express = require('express'), // The Api layer for our server
app = express(),
config = require('./config'), // configurations are loaded from here
cookieParser = require('cookie-parser'), // lets you read cookies
bodyParser = require('body-parser'), // lets you read JSONs' sent from POST request
morgan = require('morgan'); // Logging Library for express
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
var environment = config.env || 'development';
if(environment === 'dev')
app.use(morgan('dev')); // logging is only enabled in development mode : Change environments via config.js or set NODE_ENV to 'prod' or 'dev'
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json()); // initializing of modules
app.use(cookieParser());
app.use(express.static('public')); // server will serve all file from the public folder other folders aren't visible
// generic route to detect error cases such as 404 or 500 etc
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
// Define Routes here
app.get('/*',function (req, res) {
// Server index page on every request , This is done to eliminate the '#' symbol routing libraries introduce in angularjs
res.sendFile(__dirname+'/public/index.html');
});
app.listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port')); // start the server and listen to port we set in config.js
});