![]() ![]() ![]() ![]() Note: The view engine cache does not cache the contents of the template’s output, only the underlying template itself. When you make a request to the home page, the index.pug file will be rendered as HTML. ![]() You must specify the extension of the view file. Then create a route to render the index.pug file. app.set('view engine', 'pug')Ĭreate a Pug template file named index.pug in the views directory, with the following content: html Library follows this convention by mapping all of the popular Node.js template engines, and therefore works seamlessly within Express.Īfter the view engine is set, you don’t have to specify the engine or load the template engine module in your app Įxpress loads the module internally, as shown below (for the above example). Some template engines do not follow this convention. Which is called by the res.render() function to render the template code. Here is what our index.Express-compliant template engines such as Jade and Pug export a function named _express(filePath, options, callback), Return response.render( " index ", syntax. Instead of overriding that default, we will also create a folder called views.Ĭonst colors = Īpp.set( " view engine ", " pug ") // notice here we are telling express to render views using the pug templating engine. By default, express expects a folder containing templates to be called views. There are quite a few we can use with Node.js including ejs, but we will be using one called pug (formerly known as jade).īefore we introduce the pug syntax, let's first start a new project and see what we need to include. In order to do that, we need to make use of a templating engine (a tool for dynamically rendering HTML using server data). If we want to simply send HTML back we can use ndFile, but since we are using server-side templates, we will be using a method called render. So far we have only seen how to respond by sending text, but when building server-side applications, we usually want to send back HTML or JSON / XML (if we're building an RESTful API). By the end of this chapter, you should be able to: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |