Skip to content

Commit 261cb02

Browse files
author
Nicolas Fernandez Amorosino
committed
Initial commit
0 parents  commit 261cb02

File tree

9 files changed

+142
-0
lines changed

9 files changed

+142
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
3+
server.log

package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "node-web-server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"express": "^4.16.2",
14+
"hbs": "^4.0.1"
15+
}
16+
}

public/help.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Help Page</title>
8+
</head>
9+
<body>
10+
<h1>Help Page</h1>
11+
<p>Some text here</p>
12+
</body>
13+
</html>

server.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
});

views/about.hbs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Some Website</title>
8+
</head>
9+
<body>
10+
{{> header}}
11+
<p>Some text here</p>
12+
{{> footer}}
13+
</body>
14+
</html>

views/home.hbs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Some Website</title>
8+
</head>
9+
<body>
10+
{{> header}}
11+
<p>{{screamIt welcomeMessage}}</p>
12+
{{> footer}}
13+
</body>
14+
</html>

views/maintenance.hbs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Maintenance Page</title>
8+
</head>
9+
<body>
10+
<h1>We'll be right back!</h1>
11+
<p>The site is currently being updated</p>
12+
</body>
13+
</html>

views/partials/footer.hbs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<footer>
2+
<p> Created by Nicolás Amorosino - Copyright {{getCurrentYear}}</p>
3+
</footer>

views/partials/header.hbs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<header>
2+
<h1>{{pageTitle}}</h1>
3+
<p><a href="/">Home</a></p>
4+
<p><a href="/about">About</a></p>
5+
</header>

0 commit comments

Comments
 (0)