-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (43 loc) · 1.58 KB
/
app.js
File metadata and controls
53 lines (43 loc) · 1.58 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
49
50
51
52
53
'use strict';
var express = require('express');
var app = express();
var morgan = require('morgan');
var swig = require('swig');
var makesRouter = require('./routes');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
var bodyParser = require('body-parser');
var socketio = require('socket.io');
var pg = require('pg');
var conString = 'postgres://localhost:5432/twitterdb';
var client = new pg.Client(conString);
client.connect();
// templating boilerplate setup
app.set('views', path.join(__dirname, '/views')); // where to find the views
app.set('view engine', 'html'); // what file extension do our templates have
app.engine('html', swig.renderFile); // how to render html templates
swig.setDefaults({ cache: false });
// logging middleware
app.use(morgan('dev'));
// body parsing middleware
app.use(bodyParser.urlencoded({ extended: true })); // for HTML form submits
app.use(bodyParser.json()); // would be for AJAX requests
// start the server
var server = app.listen(1337, function(){
console.log('listening on port 1337');
});
var io = socketio.listen(server);
// modular routing that uses io inside it
app.use('/', makesRouter(io, client));
// the typical way to use express static middleware.
app.use(express.static(path.join(__dirname, '/public')));
// // manually-written static file middleware
// app.use(function(req, res, next){
// var mimeType = mime.lookup(req.path);
// fs.readFile('./public' + req.path, function(err, fileBuffer){
// if (err) return next();
// res.header('Content-Type', mimeType);
// res.send(fileBuffer);
// });
// });