-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-git.js
More file actions
116 lines (100 loc) · 3.82 KB
/
Copy pathserver-git.js
File metadata and controls
116 lines (100 loc) · 3.82 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Section 5, Lecture 46 -- ADDING VERSION CONTROL (GIT)
Use Git to save project,
back up work to GitHub,
and deploy project LIVE to web (not just available on localhost)
Section 5, Lecture 47 -- GITHUB and SSHKeys
-use GitBash, not the command prompt
Section 5, Lecture 48 -- DEPLOYING APPS
- ssh, gitbash, heroku
*/
const express = require('express');
const hbs = require('hbs');
const fs = require('fs');
//if process.env.port (local) does not exits, use a default 3000
const port = process.env.PORT || 3000;
var app = express();
hbs.registerHelper('getCurrentYear', function(){
return new Date().getFullYear();
});
//capitalize helper
hbs.registerHelper('screamIt', function(text){
return text.toUpperCase();
});
hbs.registerPartials(__dirname + '/views/partials');
app.set('view engine', 'hbs');
app.use(express.static(__dirname + '/public'));
// http://localhost:3000/help.html
//app.use is how you REGISTER middleware
app.use(function(req, resp, next){ //next exists so you can tell your middleware is done
var now = new Date().toString();
var log = `${now}: ${req.method} ${req.url}`;
console.log(log); //timestamp + http.method
fs.appendFile('server.log', log + '\n', function(err){
if(err){
console.log('Unable to append server.log');
}
});
next(); //app will only continue when next() is called
});
app.use('/maintenance', function(req, resp, next){
resp.render('maintenance.hbs', {
pageTitle: 'Maintenance HBS',
welcomeMessage: `This is just a maintenance page rendering:,
render hbs inside new piece of middleware, call response.render,
- no difference bw resp object in a request handler and a response object in middleware,
no need to call next(), stop it after rendering hbs file to screen`
});
var log = `${req.method} ${req.url}`;
console.log(log);
});
//^^middleware is executed in the order it is used
//help.html will still render. We set up our express.static directory, then our logger, then maintenance hbs logger
app.get('/', function(request, response){ //request = http request
response.render('home.hbs', {
pageTitle: 'Home Page',
welcomeMessage: 'Heyyaa welcome to my website'
});
});
app.get('/about', function(req, res){
res.render('about.hbs', {
pageTitle: 'About Page'
});
});
app.get('/projects', function(re, res){
res.render('projects.hbs', {
pageTitle: 'Projects Page',
welcomeMessage:'This is the last challenge of Section 5'
});
});
/*
CHALLENGE: Create a route to /bad --> send back json with an errorMessage
*/
app.get('/bad', function(req, res){
res.send({
errorMessage: 'Unable to handle request'
});
});
//The port will not be static, it will be an environment variable that heroku will set
// -heroku tells app which port to use, which changes
//app.listen(3000, function(){
app.listen(port, function(){
console.log(`Server is up and running on Port: ${port}`);
});
console.log('check the client');
//Next: Specify a script in package.json (add 'node server-git.js' cmd to the test script)
// "start": "node server.js"
// when heroku tries to start app, will not run node w/ file name, because doesnt know what it is
// so it runs the start script from package.json
//--> we run app using start script from terminal
//-- npm start
// Once we change the package.json, and this file, run git status --(returns MODIFIED FILES)
//git add . (to commit all modified)
//git commit -m "message (commit changes)
//git push --> pushes it to GitHub
//Now run cmd: heroku create (needs to be executed from INSIDE app)
// will make a real new application in our heroku webapp
//will add a new heroku remote that points to heroku git repository
// run cmd: git push heroku
// heroku open (you now have a heroku-appointed url)
//https://shrouded-headland-15611.herokuapp.com/about