-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (38 loc) · 1.63 KB
/
server.js
File metadata and controls
48 lines (38 loc) · 1.63 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
48
var express = require('express'); // express server side framework
var app = express(); // create an express instance
var mongoose = require('mongoose'); // ORM for mongodb
var morgan = require('morgan'); // Log server data to console
var bodyParser = require('body-parser'); // access POST body
var path = require('path'); // access filesystem
var multer = require('multer'); // multipart form data middleware
// Configurations
app.set('views', __dirname + "/public"); // tell server where views are found
app.engine('html', require('ejs').renderFile); // Use ejs to render html filesystem
app.set('view engine', 'html'); // Tell view engine to look for html views
// Bring in api routes for players
var api = require('./player-api');
// Connect to mlabs mongodb
mongoose.connect('mongodb://test:pass123@ds013918.mlab.com:13918/baseball_app');
var db = mongoose.connection;
db.once('open', function() {
console.log("Mongoose Connected!");
});
// Register morgan logger for dev mode
app.use(morgan('dev'));
// register and configure body-parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Register public directory for client side code and assets
app.use(express.static(path.join(__dirname, 'public')));
// Middleware to attach multipart/formdata to the request: Allow file uploads
app.use(multer({dest: './public/uploads'}).single('file'));
// Register api routes
app.use('/api', api);
// Set default index route to render the index.html file
app.get('*', function (req, res) {
res.render('index.html');
});
app.listen(process.env.PORT);
console.log("Server listening on port" + process.env.PORT);