-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (38 loc) · 1.57 KB
/
server.js
File metadata and controls
47 lines (38 loc) · 1.57 KB
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
//Load some packages. The require command returns an object which has been exported using module.exports.
//Global packages.
var path = require('path');
var express = require('express');
var mongoose = require('mongoose');
var morgan = require('morgan');
var bodyParser = require('body-parser');
//Private packages.
var config = require('./config');
//Connect to the database.
mongoose.connect(config.database, function(err, conn){
console.log('Error in db conection: ' + err);
});
//Create an express app.
var app = express();
//Log all requests to the console.
app.use(morgan('dev'));
//Use body parser so we can grab information from POST requests
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Allow CORS requests.
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
next();
});
//The files which exist in this directory are cosidered static and can be server imediatelly. This directory contains our front end.
app.use(express.static(__dirname + '/public'));
//The routes for the api.
app.use('/api', require('./app/routes/api')(app, express, config.serverKey));
//Catch all route. Muste be the last route.
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
//Start the express app.
app.listen(config.port, config.ipaddress);
console.log('Server listens on port: ' + config.port);