1
+ const express = require ( 'express' ) ;
2
+ const hbs = require ( 'hbs' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+
5
+ var app = express ( ) ;
6
+
7
+ hbs . registerPartials ( __dirname + '/views/partials' ) ;
8
+
9
+ app . set ( 'view engine' , 'hbs' ) ;
10
+
11
+ app . use ( ( req , res , next ) => {
12
+ var now = new Date ( ) . toString ( ) ;
13
+ var log = `${ now } : ${ req . method } ${ req . url } ` ;
14
+
15
+ console . log ( log ) ;
16
+
17
+ fs . appendFile ( 'server.log' , log + '\n' , ( err ) => {
18
+ if ( err ) {
19
+ console . log ( 'Unable to append to server.log' ) ;
20
+ }
21
+ } ) ;
22
+
23
+ next ( ) ;
24
+ } ) ;
25
+
26
+ // app.use((req, res) => {
27
+ // res.render('maintenance.hbs');
28
+ // });
29
+
30
+ app . use ( express . static ( __dirname + '/public' ) ) ;
31
+
32
+ hbs . registerHelper ( 'getCurrentYear' , ( ) => {
33
+ return new Date ( ) . getFullYear ( ) ;
34
+ } ) ;
35
+
36
+ hbs . registerHelper ( 'screamIt' , ( text ) => {
37
+ return text . toUpperCase ( ) ;
38
+ } ) ;
39
+
40
+ app . get ( '/' , ( req , res ) => {
41
+ res . render ( 'home.hbs' , {
42
+ pageTitle : 'Home Page' ,
43
+ welcomeMessage : 'Welcome to the Home Page!'
44
+ } ) ;
45
+ } ) ;
46
+
47
+ app . get ( '/about' , ( req , res ) => {
48
+ res . render ( 'about.hbs' , {
49
+ pageTitle : 'About Page'
50
+ } ) ;
51
+ } ) ;
52
+
53
+ app . get ( '/bad' , ( req , res ) => {
54
+ res . send ( {
55
+ errorMessage : 'Things went wrong!'
56
+ } )
57
+ } ) ;
58
+
59
+ app . listen ( 3000 , ( ) => {
60
+ console . log ( 'Server is up in port 3000' ) ;
61
+ } ) ;
0 commit comments