Nodejs Create the base application and apply the template engine _node.js

Source: Internet
Author: User

The purpose of this is to build a most basic can realize the function of the Nodejs server, can reflect the Nodejs workflow and development of the basic framework.
Requirements: Nodejs and express have been installed.
First, build the foundation of the Nodejs server (express, routing)

var express = require (' Express '); Introduction of the Express module 
var app = Express ();//Call the Express () function to initialize the function 
app.get ('/stooges/:name? ', Functions (req, RES, Next) {//Set the first route, expecting a name to be entered 
 var name = Req.params.name;//Get the input name, req.params 
 switch (name?name.tolowercase ( ): ' {//judge the name by 
 ' Larry ': Case ' 
 Curly ': Case 
 ' moe ': 
  res.send (name + ' is my favorite stooge. ');//Compliant Conditions use Res.send to send information to break 
 ; 
 Default: Next () the  
  //next () function, which also has parameter passing in the function, means that if this route passes a parameter that is not enough to perform this route, the next () function means to go to the next function (this is the route). 
} 
}); 
App.get ('/stooges/*? '), function () {//here? Indicates that the last parameter may or may not be the same as the previous route 
 Res.send (' no Stooges listed '); 
}); 
App.get ('/? ', function (req,res) {//None of the time default routing 
 res.send (' Hello World '); 
var port = 8080; Set up and monitor the ports 
App.listen (port); 

Second, the use of Jade template engine, add template rendering

var express = require (' Express '); 
var app = Express (); 
The following three sentences complete the set of view, including engine, template path, and other settings 
App.set (' View engine ', ' Jade '); 
App.set (' view options ', {layout:true}); 
App.set (' views ', __dirname + '/views '); 
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}); To render the view, the incoming template name can break 
 ; 
 Default:  
  next (); 
} 
}); 
App.get ('/stooges/*? ', Function (req, res) { 
 res.render (' stooges ', {stooges:null}); 
}); 
App.get ('/? ', Function (req, res) { 
 res.render (' index '); 
}); 
var port = 8080; 
App.listen (port); 

A total of three template files, Layout.jade (layout file), Index.jade and Stooges.jade, three template file code are as follows:
Layout.jade

!!! 5//Representative document type is HTML5
html (lang= "en")
head
 Title I Web Site block
 scripts block
content
 

Index.jade

Entends layout block
content
Hi-Hello World
 

Stooges.jade

Extends layout block
content
if (stooge)
 p #{stooge} are my favorite stooge.//#{stooge here Gets the parameter that is passed in when the JS renders the template
Else
 P No stooges listed 

With the above code, you can use Node.js and express to build a base node application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.