Skip to content

Commit a04c7e7

Browse files
author
Natalie
authored
Merge pull request #30 from fac-12/expressHbsSetup
Express hbs setup
2 parents 42ae995 + f65abf5 commit a04c7e7

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "node ./src/tests/*.test.js | tap-spec",
7+
"test": "node src/tests/routes.test.js | tap-spec",
88
"nodemon": "nodemon src/index.js",
99
"start": "node src/index.js"
1010
},

src/app.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const express = require('express')
2+
const path = require('path');
3+
const bodyParser = require('body-parser');
4+
const exphbs = require('express-handlebars');
5+
6+
const router = require('./controllers/index');
7+
const controllers = require('./controllers');
8+
9+
const app = express();
10+
11+
app.use(router);
12+
app.use(controllers);
13+
app.use(bodyParser.json());
14+
app.use(bodyParser.urlencoded({ extended: false }));
15+
app.use(
16+
express.static(path.join(__dirname, '..', 'public'))
17+
);
18+
19+
app.set('port', process.env.PORT || 3000);
20+
app.set('views', path.join(__dirname, 'views'));
21+
app.set('view engine', 'hbs');
22+
23+
app.engine(
24+
'hbs',
25+
exphbs({
26+
extname: 'hbs',
27+
layoutsDir: path.join(__dirname, 'views', 'layouts'),
28+
partialsDir: path.join(__dirname, 'views', 'partials'),
29+
defaultLayout: 'main'
30+
})
31+
);
32+
33+
module.exports = app;

src/controllers/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const router = require('express').Router();
2+
3+
router.get('/', (req, res) => {
4+
res.render('home');
5+
})
6+
7+
module.exports = router;

src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const app = require('./app')
2+
3+
app.listen(app.get('port'), () => {
4+
console.log('App is running on port', app.get('port'))
5+
});

src/tests/routes.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const test = require('tape');
2+
const request = require('supertest');
3+
const app = require('../app');
4+
5+
const endpoints = ['/',
6+
'/search',
7+
'/search/:category',
8+
'search/:category/:place',
9+
'/search/:category/list',
10+
'/search/:category/list/:place',
11+
'/add',
12+
'/add/exists',
13+
'/add/details',
14+
'add/details/success'];
15+
16+
endpoints.forEach(endpoint => {
17+
test('All routes should return status code 200 and text/html', t => {
18+
request(app)
19+
.get(`${endpoint}`)
20+
.expect(200)
21+
.expect('Content-Type', 'text/html; charset=utf-8')
22+
.end(err => {
23+
t.error(err);
24+
t.end();
25+
})
26+
})
27+
})

src/views/layouts/main.hbs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
{{{ body }}}

0 commit comments

Comments
 (0)