服务端语言对比
优势 | 劣势 | |
---|---|---|
java | 功能强大,生态完善,性能优越,适合大型应用开发 | 开发效率一般,构建部署繁琐 |
php | 世界上最好的语言。快速高效,经济型公司首选 | 弱类型,复杂应用疲软 |
node.js | 性能好,上手简单 | 技术生态不够完善 |
执行斐波那契数列耗时(ms) | |
---|---|
java | 782 |
javascript | 384 |
php | 1222 |
python | 3809 |
c# | 3217 |
c++ | 641 |
go | 548 |
nodejs简介
简单的说 Node.js 就是运行在服务端的 JavaScript。
Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好。
安装
官网:nodejs.cn/
第一个应用
const http = require("http");
http
.createServer(function (request, response) {
response.end("Hello World");
})
.listen(8080);
复制代码
egg.js
Egg 是一个继承于 Koa 的 node.js 框架。Egg 按照约定进行开发,奉行『约定优于配置』,使团队协作成本降低
安装
npm init egg
npm install
npm run dev
复制代码
一个基础的 API 大致由
路由
、获取请求参数
、逻辑处理
、返回响应数据
几部分组成
路由
app/router.js
用于配置 URL 路由规则
router.get("/", controller.home.index);
// 当访问GET '/' ,app/controller/home.js 下的 index 方法会执行
router.post("/create", controller.user.create);
// 当访问POST '/create' ,app/controller/user.js 下的 create 方法会执行
复制代码
获取请求参数
this.ctx.query
获取 URL 中 ? 后面的部分参数
// GET /posts?category=egg&language=node
// app/controller/post.js
class PostController extends Controller {
async listPosts() {
const query = this.ctx.query;
// {
// category: 'egg',
// language: 'node',
// }
}
}
复制代码
this.ctx.params
获取路由中的动态参数
// app.get('/projects/:projectId/app/:appId', controller.app.listApp);
// GET /projects/1/app/2
class AppController extends Controller {
async listApp() {
const params = this.ctx.params;
// {
// projectId: '1',
// appId: '2'
// }
}
}
复制代码
this.ctx.request.body
获取 body 参数
// POST /api/posts HTTP/1.1
// Host: localhost:3000
// Content-Type: application/json; charset=UTF-8
//
// {"title": "controller", "content": "what is controller"}
class PostController extends Controller {
async listPosts() {
const body = this.ctx.request.body;
// {
// title: 'controller',
// content: 'what is controller'
// }
}
}
复制代码
返回响应数据
this.ctx.body
返回响应数据
class ViewController extends Controller {
async show() {
// 返回Content-Type为application/json的body
this.ctx.body = {
name: "egg",
category: "framework",
language: "Node.js",
};
}
async page() {
// 返回Content-Type为text/html的body
this.ctx.body = "<html><h1>Hello</h1></html>";
}
}
复制代码
连接 mysql 数据库
- 安装 mysql 插件
npm i egg-mysql
复制代码
- 配置
// config/plugin.js
exports.mysql = {
enable: true,
package: "egg-mysql",
};
复制代码
// config/config.${env}.js
exports.mysql = {
// 单数据库信息配置
client: {
// host
host: "localhost",
// 端口号
port: "3306",
// 用户名
user: "root",
// 密码
password: "root",
// 数据库名
database: "database",
},
};
复制代码
- 使用
// 查找id 为 ${uid}的用户
await this.app.mysql.get("users", { id: uid });
复制代码
处理业务逻辑
业务逻辑建议放在app/service
中,当然也包括数据库的操作
// app/service/user.js
const Service = require("egg").Service;
class UserService extends Service {
async find(uid) {
// 假如 我们拿到用户 id 从数据库获取用户详细信息
const user = await this.app.mysql.get("users", { id: uid });
return user;
}
}
module.exports = UserService;
复制代码
之后可以通过 Controller 获取 Service 层拿到的数据。
// app/controller/user.js
class UserController extends Controller {
async info() {
const ctx = this.ctx;
const userId = ctx.params.id;
// 调用service层的user下的find方法
const user = await ctx.service.user.find(userId);
ctx.body = user;
}
}
复制代码
基本 CURD 语句可以使用 create、get、select、update、delete 方法
直接执行 sql 语句可使用 query 方法
其他
事务的控制
sequelize
sequelize 是一个广泛使用的 ORM 框架,它支持 MySQL、PostgreSQL、SQLite 和 MSSQL 等多个数据源。
helper
Helper 函数用来提供一些实用的 utility 函数。
// extend/helper.js
// 处理成功响应
exports.success = ({ ctx, res = null, msg = '请求成功' }) => {
ctx.body = {
code: 0,
data: res,
msg
}
ctx.status = 200
}
复制代码
异常处理中间件
ctx.throw(404, 'this is error msg')
抛出的异常可以在中间件中统一处理
module.exports = (option, app) => {
return async function (ctx, next) {
try {
await next()
} catch (err) {
// 所有的异常都在 app 上触发一个 error 事件,框架会记录一条错误日志
app.emit('error', err, this)
const status = err.status || 500
// 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息
const error = status === 500 && app.config.env === 'prod' ?
'Internal Server Error' :
err.message
// 从 error 对象上读出各个属性,设置到响应中
ctx.body = {
code: status, // 服务端自身的处理逻辑错误(包含框架错误500 及 自定义业务逻辑错误533开始 ) 客户端请求参数导致的错误(4xx开始),设置不同的状态码
error: error
}
if (status === 422) {
ctx.body.detail = err.errors
}
ctx.status = 200
}
}
}
复制代码
eggjs暂时介绍到这里,详细的文档请前往官网学习
近期评论