-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (43 loc) · 1.35 KB
/
app.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
/* Environment Configuration */
// This will not override system-set variables.
require( 'apprequire' )( __dirname );
require( 'dotenv' ).config();
/* Dependencies */
const express = require( 'express' );
const path = require( 'path' );
const bodyParser = require( 'body-parser' );
/* Server Configuration */
const app = express();
app.set( 'views', path.join( __dirname, 'views' ) ); // __dirname is the directory a file resides in
app.set( 'view engine', 'pug' );
// set any variables that must
// be passed to pug files here
app.use( ( req, { locals }, next ) => {
locals.env = process.env.NODE_ENV; // eslint-disable-line
next();
} );
// serve resources in /public with /static
app.use( '/static', express.static( path.join( __dirname, 'public' ) ) );
// parse all of our requests into JSON
app.use( bodyParser.json() );
// has to do with library extension. Must be false
app.use( bodyParser.urlencoded( {
extended : false,
} ) );
/* Routing */
const index = require( './routes/index' );
app.use( '/', index );
/* Error Responses */
app.use( ( req, res ) => {
res.render( 'error', {
message : 'We were unable to find this page',
status : 404,
error : {},
title : '404',
} );
} );
/* Final Server Initialization */
const port = process.env.PORT;
app.listen( port, () => {
console.log( `App running on port: ${port}` );
} );