This article describes how to create a basic application and apply the template engine in NodeJS. For more information, see create a node. js server with the most basic functions, it can reflect the node. js workflow and the basic development framework.
Requirement: nodejs and express have been installed.
1. Build a basic NodeJS server (express and route)
Var express = require ('express '); // introduce the express module var app = express (); // call the express () function to initialize the function. get ('/stooges/: name? ', Function (req, res, next) {// sets the first route, expect a name to be input var name = req. params. name; // obtain the input name, req. params switch (name? Name. toLowerCase (): '') {// judge the name. case 'Larry ': case 'curly': case 'moe': res. send (name + 'is my favorite stooge. '); // use res to meet the conditions. send the message break; default: next (); // The next () function. parameters are also transmitted in the function, it indicates that if the route cannot be executed due to insufficient parameters passed by the route, the next () function will jump to the next function to continue executing (here is the route)}); app. get ('/stooges /*? ', Function () {// here? It indicates whether or not the last parameter can exist. The same routing as the preceding one is res. send ('no stooges listed') ;}); app. get ('/? ', Function (req, res) {// default route res when nothing exists. send ('Hello World') ;}); var port = 8080; // set and listen to the app on the port. listen (port); console. log ('listensing on port' + port );
Ii. Use the Jade template engine to add template Rendering
Var express = require ('express '); var app = express (); // The following three statements have completed the view settings, including the engine, template path, and other settings app. set ('view engine ', 'jade'); app. set ('view options', {layout: true}); app. set ('view', _ dirname + '/view'); app. get ('/stooges/: name? ', Function (req, res, next) {var name = req. params. name; switch (name? Name. toLowerCase (): '') {case 'Larry ': case 'curly': case 'moe': res. render ('stooges', {stooge: name}); // render the view. input the Template name To break; default: next () ;}}); app. get ('/stooges /*? ', Function (req, res) {res. render ('stoogies', {stooges: null}) ;}); app. get ('/? ', Function (req, res) {res. render ('index') ;}); var port = 8080; app. listen (port); console. log ('listensing on port' + port );
There are a total of three template files: layout. jade (layout file), index. jade and stooges. jade. The code of the three template files is as follows:
Layout. jade
!!! 5 // indicates that the document type is HTML5html (lang = "en") head title My Web Site block scriptsblock content
Index. jade
entends layoutblock contenthi hello world
Stooges. jade
Extends layoutblock contentif (stooge) p # {stooge} is my favorite stooge. // here # {stooge} gets the parameter Else p no stooges listed passed in when the js rendering Template
With the above code, you can use node. js and express to build a basic node application.