express.js的介绍及使用

  • Node.js 《Node.js 官网(中文)》
  • Node.js 《Node.js 官网(英文)》

<br/><br/>

Node.js 是什么

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
1. JavaScript 运行时
2. 既不是语言,也不是框架,它是一个平台

Node.js 中的 JavaScript

1. 没有 BOM、DOM
2. 在 Node 中为 JavaScript 提供了一些服务器级别的 API
    2.1 fs 文件操作模块
    2.2 http 网络服务构建模块
    2.3 os 操作系统信息模块
    2.4 path 路径处理模块
    2.5 .....

<!-- markdown-to-slides share1.md -o index.html -s slide.css -->

I. express简介

基于 Node.js 平台,快速、开放、极简的 Web 开发框架

简单来说,封装了node中http核心模块,专注于业务逻辑的开发。

安装方法

npm install express --save

koa

Koa - next generation web framework for node.js

Koa 是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小、更富有表现力、更健壮的基石。 通过利用 async 函数,Koa 帮你丢弃回调函数,并有力地增强错误处理。 Koa 并没有捆绑任何中间件, 而是提供了一套优雅的方法,帮助您快速而愉快地编写服务端应用程序。(project:koa-1.js)

const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);

<img src="

Hello XXX (express)

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('hello home ...')
})

app.get('/about', function (req, res) {
  res.send('hello about ...')
})

app.listen(3000, function () {
  console.log('express app is runing .....')
})

project:demo-2.js Hello XXX 测试结果:

<img src="

相关推荐