NodeJS practice-create a basic application and apply the template engine
The purpose of this project is to build a node. js server with the most basic functions and functions, which can reflect the node. js workflow and the basic framework for development.
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. paramsswitch (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 HTML5
Html (lang = "en ")
Head
Title My 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} is my favorite stooge. // here # {stooge} obtains the parameters passed in when the js rendering template is used.
Else
P no stooges listed
With the above code, you can use node. js and express to build a basic node application.