安装
Express JS 是使用 Node 开发 Web Applications,APIs 和几乎任何类型的 Backend 的 goto 框架。
要安装 express,你只需运行 npm 命令即可
npm install express --save
而且你已经完成了。
创建和运行新的快速服务器
创建文件 app.js 并添加此代码
// require express
var express = require('express');
var app = express();
// when "/" is opened in url, this function will be called.
app.get('/', function (req, res) {
res.json({ code: 200, message: 'success' });
})
app.listen( 3000, function () {
console.log('Express server running at http://localhost:3000');
});
- 在你的终端中,运行
node app.js和 - 在 Web 浏览器中打开 url
http://localhost:3000以查看新创建的快速服务器。
安装 body-parser 和 express-session 以及 express 也是一个好主意,因为大多数时候你都想读取 POST 请求中发送的数据并管理用户会话。