Table of Contents
1. 生成Express项目
2. Jade 模板引擎
3. 回头看看 app.js
4. 编写路由规则
5. 瞧瞧 jade 模板视图文件
6. 依次创建 detail 、 list 、 admin 页面
Home Backend Development PHP Tutorial 初步尝试Express&jade快速建站

初步尝试Express&jade快速建站

Jun 20, 2016 pm 12:37 PM

最近一直在看Web开发相关的书籍,一周时间读完了《Node开发指南》一书,由于之前了解过一些Node的相关知识,于是前面基础知识学得比较快,到开发实践这一章的时候,却发现好多内容不能跟着练习了。因为近几年Node的发展过于快速,其Web开发框架 Express 必然也更新得比较频繁,并且版本与版本之间差异较大。所以,书中必然有好多代码在现在来看肯定运行不了,但是大体的框架还是在那里,想动手实践还是得对照着官方文档进行。

1. 生成Express项目

根据 官方文档入门 的介绍,根据Express建站的步骤大致写一下:

  1. npm install express 安装express
  2. npm install express-generator -g 安装express应用生成器
  3. express myapp 生成express项目目录以及基本启动代码
  4. DEBUG=myapp npm start 启动应用(Mac or Linux); set DEBUG=myapp & npm start 启动应用(Windows)
  5. 浏览器访问 http://localhost:3000

通过上面几步很快就完成了一个项目的部署,那么接下来就是要往里填写内容了,用框架开发就是这么快,但是会忽略掉很多重要的细节。

打开 myapp 文件夹,会看到该项目的目录结构如下:

.├── app.js├── bin│   └── www├── package.json├── public│   ├── images│   ├── javascripts│   └── stylesheets│       └── style.css├── routes│   ├── index.js│   └── users.js└── views    ├── error.jade    ├── index.jade    └── layout.jade
Copy after login

了解过一些Node的相关知识就知道,Node是通过动态网页的方式来实现网站的,就如同其他语言Java、PHP一样,要实现动态页面就要在HTML模板中插入程序代码,于是就有了JSP、PHP以及ASP等技术。通过JavaScript实现的模板引擎有很多, Jade 就是其中之一,为什么选择它,因为Express默认就是它,反正也是新接触,那就它咯。

2. Jade 模板引擎

由于新学,也没有什么经验可谈,基本用法参考官网就可以了。这类语言基本上看一遍就百分之八九十了,接下来就是熟练使用它。

  • Jade Template Syntax Documentation by Example
  • Jade —— 源于 Node.js 的 HTML 模板引擎 - 新闻 - SegmentFault

3. 回头看看 app.js

// view engine setupapp.set('views', path.join(__dirname, 'views'));app.set('view engine', 'jade');
Copy after login

这里就是设置 Jade 模板引擎以及视图目录的地方。

4. 编写路由规则

编写路由规则,打开 routes/index.js 文件,添加4条路由规则,代表4个不同的页面。

/* GET home page. */router.get('/', function(req, res, next) {  res.render('index', { title: 'Home Page'});});/* GET detail page. */router.get('/detail/:id', function(req, res, next) {  res.render('detail', { title: 'Detail Page'});});/* GET admin page. */router.get('/admin', function(req, res, next) {  res.render('admin', { title: 'Admin Page'});});/* GET list page. */router.get('/list', function(req, res, next) {  res.render('list', { title: 'List Page'});});
Copy after login

5. 瞧瞧 jade 模板视图文件

打开 views/layout.jade

doctype htmlhtml  head    title= title    link(rel='stylesheet', href='/stylesheets/style.css')  body    block content
Copy after login

看了一遍 jade 语法的基本就能看出这是一个基本页面,然后再打开 views/index.jade 。

extends layoutblock content  h1= title  p Welcome to #{title}
Copy after login

路由规则中, title 变量就用与此处,如此即为动态页面。 npm start 启动应用,访问 http://localhost:3000 得到页面如下图。

6. 依次创建 detail 、 list 、 admin 页面

根据路由规则所规定的访问路径,依次访问,会得到不同的动态页面。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles