Skip to content

Commit

Permalink
refactor: subchapter 6.1 with nunjucks
Browse files Browse the repository at this point in the history
  • Loading branch information
wbruno committed May 7, 2017
1 parent c3f6df3 commit 55f6b2b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 2,547 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
package-lock.json

# min files
public
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Execute a partir do diretório em que você for escrever a aplicação, pois ser

```
$ npm i bluebird body-parser config debug express jwt-simple method-override moment
$ npm i jshint mongojs mongoose mocha passport passport-http cors swig json2xml hbs jade
$ npm i gulp gulp-concat gulp-minify-css gulp-uglify gulp-livereload gulp-util
$ npm i jshint mongojs mongoose mocha passport passport-http cors swig nunjucks json2xml hbs jade
$ npm i gulp gulp-concat gulp-minify-css gulp-clean-css gulp-uglify gulp-livereload gulp-util
$ npm i grunt grunt-contrib-watch grunt-contrib-uglify grunt-contrib-cssmin
$ npm i -g mocha istanbul gulp grunt nodemon
```
Expand Down
19 changes: 12 additions & 7 deletions capitulo_6/capitulo_6.1/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
*/

'use strict';
var express = require('express'),
path = require('path'),
swig = require('swig'),
debug = require('debug')('livro_nodejs:app'),
app = express();
const express = require('express');
const path = require('path');
const nunjucks = require('nunjucks');
const debug = require('debug')('livro_nodejs:app');
const app = express();

// config
app.use(express.static(path.join(__dirname, 'public')));

app.set('view engine', 'html');
app.set('views', path.join(__dirname, 'views'));
app.engine('html', swig.renderFile);

nunjucks.configure('views', {
autoescape: true,
express: app,
tags: ''
});

// routes
app.use('/', require('./routes'));

// errors handling
app.use(function(request, response, next) {
var err = new Error('Not Found');
let err = new Error('Not Found');
err.status = 404;
next(err);
});
Expand Down
2 changes: 1 addition & 1 deletion capitulo_6/capitulo_6.1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"debug": "^2.2.0",
"express": "^4.12.4",
"json2xml": "^0.1.1",
"swig": "^1.4.2"
"nunjucks": "^3.0.0"
},
"devDependencies": {
"gulp": "^3.9.0",
Expand Down
11 changes: 6 additions & 5 deletions capitulo_6/capitulo_6.1/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var express = require('express'),
router = express.Router();
const express = require('express');
const router = express.Router();

router.get('/', function (request, response) {
response.render('index', { title: 'Stormtroopers API' });
Expand All @@ -12,7 +12,8 @@ router.get('/loop', function (request, response) {
{ name: 'Episode IV: A New Hope'},
{ name: 'Episode V: The Empire Strikes Back'},
{ name: 'Episode VI: Return of the Jedi'},
{ name: 'Episode VII: The Force Awakens'}
{ name: 'Episode VII: The Force Awakens'},
{ name: 'Episode VIII: The Last Jedi'}
]});
});
router.get('/if', function (request, response) {
Expand All @@ -24,10 +25,10 @@ router.get('/xml', function (request, response) {
response.send('<?xml version="1.0" encoding="UTF-8"?><characters><character><name>Boba Fett</name><homeworld>Kamino</homeworld></character><character><name>Jango Fett</name><homeworld>Concord Dawn</homeworld></character><character><name>Chewbacca</name><homeworld>Kashyyyk</homeworld></character></characters>');
});

var json2xml = require('json2xml');
let json2xml = require('json2xml');
router.get('/xml-mapper', function (request, response) {
response.header('Content-Type','text/xml');
var obj = { "characters": [
let obj = { "characters": [
{ "character": { "name": "Boba Fett", "homeworld": "Kamino" } },
{ "character": { "name": "Jango Fett", "homeworld": "Concord Dawn" } },
{ "character": { "name": "Chewbacca", "homeworld": "Kashyyyk" } }
Expand Down
Loading

0 comments on commit 55f6b2b

Please sign in to comment.