-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (66 loc) · 3.25 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
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
//-----------------------------------------------------------------------------------------------
//Set up
//-----------------------------------------------------------------------------------------------
var express = require('express');
// create our app w/ express
var app = express();
/*Gzip compressing can greatly decrease the size of the response body and hence increase
the speed of a web app. Use the compression middleware for gzip compression in your Express app.*/
var compression = require('compression');
// mongoose for mongodb
var mongoose = require('mongoose');
// log requests to the console (express4)
var morgan = require('morgan');
// pull information from HTML POST (express4)
var bodyParser = require('body-parser');
var appRoutes = require('./app/routes/routes');
/*Multer is a node.js middleware for handling multipart/form-data, which is primarily used
for uploading files. It is written on top of busboy for maximum efficiency.*/
var multer = require('multer');
/*Express middleware for reaping uploaded files saved to disk by multer or any multipart
middleware propagating the req.files object. The middleware will automatically remove any
uploaded files left in their temporary location upon response end or close.*/
var autoReap = require('multer-autoreap');
var oneDay = 86400000;
var port = process.env.PORT || 3000;
//-----------------------------------------------------------------------------------------------
//Configuration
//-----------------------------------------------------------------------------------------------
var database = require('./config/database');
mongoose.connect(database.url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Successfully connected to the database!");
});
//static content is compressed using gzip
app.use(compression())
// set the static files location /public/img will be /img for users
//app.use(express.static(__dirname + '/public', { maxAge: oneDay }));
app.use(express.static(__dirname + '/public'));
//app.use('/image', express.static(__dirname + '/public/img'));
// *log every request to the console*
app.use(morgan('dev'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({'extended':'true'}));
// parse application/json
app.use(bodyParser.json());
//allow cross origin requests
app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
res.header("Access-Control-Allow-Origin", "http://localhost");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
//app.use(autoReap);
//-----------------------------------------------------------------------------------------------
//Load the routes
//-----------------------------------------------------------------------------------------------
app.use('/api', appRoutes);
//-----------------------------------------------------------------------------------------------
//listen (start app with node server.js)
//-----------------------------------------------------------------------------------------------
app.listen(port, function (){
console.log("Portfolio App listening on port " + port);
});