Mittwoch, 18. Mai, 2016
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Delving into Node.js and Express web framework 41<br />
1 exports.render = function(req, res) {<br />
2 res.render('index', {<br />
3 title: 'Howdy World'<br />
4 })<br />
5 };<br />
The res.render() method will use the EJS template engine to look for the file in the views folder that<br />
we set in the config/express.js file and will then render the view (‘index’ - without .ejs extension)<br />
using the object variables that we send to it (just title for now).<br />
Using static files<br />
You will most probably need to show some images in your templates, and the way you can do this<br />
is by using the express.static()middleware. In the config/express.js file add the following line just<br />
before the return app; statement:<br />
1 app.use(express.static('./public'));<br />
The express.static() middleware takes one argument to determine the location of the static folder.<br />
Lets say you added an image named logo.jpg to the img folder (inside the public folder). The way<br />
you would show it in a view (index.ejs) is by using the following straight forward code:<br />
1 <br />
Conclusion<br />
Yay, we’re done! In this tutorial we’ve covered a lot of ground so don’t worry if some of it doesn’t<br />
sink in at first reading. Reread, explore on your own - that’s how you’ll get the grip of it the best.<br />
Anyways, enough of the self help punch lines, hope you liked the post and see you in the next<br />
installment of this post when we’ll tackle the MongoDB.