


NodeJS creates a basic application and applies the template engine_node.js
The purpose of this time is to build a most basic NodeJS server that can realize functions and reflect the workflow of NodeJS and the basic framework of development.
Requirements: nodejs and express have been installed.
1. Build a basic NodeJS server (express, routing)
var express = require(‘express'); //引入express模块 var app = express(); //调用express()函数,对函数进行初始化 app.get('/stooges/:name?', function(req, res, next){ //设置第一个路由,期待一个name被输入 var name = req.params.name; //获取输入的名字,req.params switch(name?name.toLowerCase():' '){ //对名字进行判断 case ‘larry': case ‘curly': case ‘moe': res.send(name + 'is my favorite stooge.'); //符合条件利用res.send发送信息 break; default: next(); //next()函数,在function中也有参数传递,它的含义是如果这个路由传递的参数不够不能执行这个路由的话,next()函数表示跳转到下一个函数继续执行(这里是路由) } }); app.get(‘/stooges/*?', function(){ //这里?表示最后的参数可以有也可以没有,同上一个路由相同 res.send(‘no stooges listed'); }); app.get(‘/?', function(req,res){ //什么都没有的时候默认的路由 res.send(‘hello world'); }); var port = 8080; //设定并对端口进行监听 app.listen(port); console.log(‘Listensing on port' + port);
2. Use Jade template engine and add template rendering
var express = require(‘express'); var app = express(); //下面三句话完成了对view的设定,包括引擎、模板路径以及其他设定 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}); //进行视图的渲染,传入模板名即可 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); console.log(‘Listensing on port' + port);
There are three template files in total, namely layout.jade (layout file), index.jade and stooges.jade. The codes of the three template files are as follows:
layout.jade
!!! 5 //代表文档类型是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. //这里的#{stooge}获取了js渲染模板时传递进来的参数 Else p no stooges listed
With the above code, you can use node.js and express to build a basic node application.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
