Express 快速起步
简单的Hello
确保已经完成 Node.js 安装,现在创建目录(myapp)来保存应用(假设应用名是
myapp
):
mkdir myapp
cd myapp
在目录下创建一个简单的
index.js
:
index.js
const express = require('express');
const server = express();
const port = 3000;
server.get('/hello', function(req, res) {
res.send('Hello World!');
});
server.listen(port, function() {
console.log('Listening on ' + port);
});
一种方式是 : 执行
npm install express@4
安装 Express 4系列,执行以后,目录下会生成package.json
内容非常简单:
package.json
{
"dependencies": {
"express": "^4.21.2"
}
}
现在可以运行
index.js
了:
index.js
node index.js
输出非常简单:
index.js
终端输出Listening on 3000
此时用浏览器访问 http://127.0.0.1:3000 会提示 Cannot GET /
,但是可以注意到这个简单 index.js
实际上是提供了 /hello
入口,所以应该访问 http://127.0.0.1:3000/hello ,就能够看到网页输出 Hello World!
get 请求 /hello => res 返回 Hello World!
交互方式创建项目
另一种方式是 : 执行
npm init
在程序目录下生成一个package.json
文件(这是一个交互过程,根据交互内容生成配置)
生成的 package.json
内容类似如下:
npm init
生成一个初始化 package.json
的内容{
"name": "myapp",
"version": "1.0.0",
"description": "test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
其中主要的就是 index.js
作为主文件
安装 Express:
npm install express
完成Express安装以后,目录下的 package.json
会修订添加 dependencies
部分:
package.json
配置{
"name": "myapp",
"version": "1.0.0",
"description": "test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.21.2"
}
}
此时目录下还有一个 package-lock.json
是自动生成的,是为了解决依赖的详细版本要求
快速脚手架
全局安装npm包
express-generator
,然后创建应用(使用的 view engine 是Pug
):
express-generator
npm install -g express-generator
有了
express-generator
就能快速建立myapp
应用脚手架:
express --view=pug myapp
此时输出信息显示自动创建了程序目录及相关文件:
create : myapp/
create : myapp/public/
create : myapp/public/javascripts/
create : myapp/public/images/
create : myapp/public/stylesheets/
create : myapp/public/stylesheets/style.css
create : myapp/routes/
create : myapp/routes/index.js
create : myapp/routes/users.js
create : myapp/views/
create : myapp/views/error.pug
create : myapp/views/index.pug
create : myapp/views/layout.pug
create : myapp/app.js
create : myapp/package.json
create : myapp/bin/
create : myapp/bin/www
change directory:
$ cd myapp
install dependencies:
$ npm install
run the app:
$ DEBUG=myapp:* npm start
进入目录安装依赖:
cd myapp
npm install
可能会提示存在漏洞,按照提示使用 npm audit fix --force
修复
运行:
DEBUG=myapp:* npm start
npx
一次性执行脚本
在安装了 Node.js 之后,同时也具备了 npx
。这个工具可以从仓库执行任何软件包而无需安装。这主要是用于测试一些一次性代码,例如使用脚手架脚本来初始化项目,但既不是依赖项也不是开发依赖项。