青云志2百度云盘资源资源 求助

966,690 六月 独立访问用户
语言 & 开发
架构 & 设计
文化 & 方法
您目前处于:
向未来兼容——ES2015+ / TypeScript 开发 Node.js 项目
向未来兼容——ES2015+ / TypeScript 开发 Node.js 项目
智能化运维、Serverless、DevOps......2017年有哪些最新运维技术趋势?!
使用 ES2015+ 特性开发项目可以大大提高开发效率,尤其是使用 async/await 特性来解决异步的问题,借助 Babel 编译,现在就可以使用未来的一些特性。接着介绍如何解决断点调试编译后的代码,以及如何将错误信息定位到源代码下。最后介绍使用 TypeScript/Flow 类型推断和检测来辅助大型项目的开发。
李成银,网名 welefen,360奇舞团高级工程师,技术经理。Node.js 开源框架 ThinkJS 作者,教育部 HTML5 培训认证讲师,擅长性能优化和前端工程化解决方案。
QCon是由InfoQ主办的全球顶级技术盛会,每年在伦敦、北京、东京、纽约、圣保罗、杭州、旧金山召开。自2007年3月份首次举办以来,已经有包括传统制造、金融、电信、互联网、航空航天等领域的近万名架构师、项目经理、团队领导者和高级开发人员参加过QCon大会。
相关厂商内容
相关赞助商
CNUTCon全球运维技术大会,9月10日-9月11日,上海&光大会展中心大酒店,
告诉我们您的想法
允许的HTML标签: a,b,br,blockquote,i,li,pre,u,ul,p
当有人回复此评论时请E-mail通知我
允许的HTML标签: a,b,br,blockquote,i,li,pre,u,ul,p
当有人回复此评论时请E-mail通知我
允许的HTML标签: a,b,br,blockquote,i,li,pre,u,ul,p
当有人回复此评论时请E-mail通知我
赞助商链接
架构 & 设计
文化 & 方法
<及所有内容,版权所有 &#169;
C4Media Inc.
服务器由 提供, 我们最信赖的ISP伙伴。
北京创新网媒广告有限公司
京ICP备号-7
找回密码....
InfoQ账号使用的E-mail
关注你最喜爱的话题和作者
快速浏览网站内你所感兴趣话题的精选内容。
内容自由定制
选择想要阅读的主题和喜爱的作者定制自己的新闻源。
设置通知机制以获取内容更新对您而言是否重要
注意:如果要修改您的邮箱,我们将会发送确认邮件到您原来的邮箱。
使用现有的公司名称
修改公司名称为:
公司性质:
使用现有的公司性质
修改公司性质为:
使用现有的公司规模
修改公司规模为:
使用现在的国家
使用现在的省份
Subscribe to our newsletter?
Subscribe to our industry email notices?
我们发现您在使用ad blocker。
我们理解您使用ad blocker的初衷,但为了保证InfoQ能够继续以免费方式为您服务,我们需要您的支持。InfoQ绝不会在未经您许可的情况下将您的数据提供给第三方。我们仅将其用于向读者发送相关广告内容。请您将InfoQ添加至白名单,感谢您的理解与支持。Android+Arduino开发专题(15)
创建package.json
建一个目录的文件夹,起一个你喜欢的名字。
node项目嘛,当然要先建立一个package.json
跟着提示下一步就好了。
创建 tsconfig.json
typescript 的项目都需要一个tsconfig.json
会创建一个这样内容的tsconfig.json
&&&&&compilerOptions&:
&&&&&&&&&module&:
&commonjs&,
&&&&&&&&&target&:
&&&&&&&&&noImplicitAny&:
&&&&&&&&&sourceMap&:
&&&&&exclude&:
&&&&&&&&&node_modules&
因为写node.js你可以修改tagget为es6, 要调试把 sourceMap改为true, 添加allowjs为true,就可以ts和js混合玩了.
&&&&&compilerOptions&:
&&&&&&&&&module&:
&commonjs&,
&&&&&&&&&target&:
&&&&&&&&&noImplicitAny&:
&&&&&&&&&sourceMap&:
&&&&&&&&&allowJs&:
&&&&&exclude&:
&&&&&&&&&node_modules&
安装 node 的 .d.ts 库
还是因为是node.js开发,我们要安装node的ts库, 为了强类型,主要还是为了智能感知。
install dt~node --global
国内的用typings 的时候,而且就会抽风,好久都完成不了,不要紧,Ctrl&#43;C掉了,再执行,多试几次,总会有成功的时候的。
配置 vscode
打开 vscode
话 也可以右键菜单,open with Code.
配置 TypeScript 编译
按ctrl &#43; shift &#43; b, 如果没有配置过,task, 就会在上面提示。
选择【配置任务运行程序】
会在.vscode文件夹下生成一个 task.json, 基本不用动,我到现在是没有改过它。
&&&&&version&:
&&&&&command&:
&&&&&isShellCommand&:
&&&&&args&:
&&&&&showOutput&:
&&&&&problemMatcher&:
我们来创建一个app.ts 文件,随便写点东西吧
&&&&constructor()
&&&&public
&&&&&&&&console.log('this
is a person')
p.print();
按Ctrl&#43;Shift&#43;B,编译一下,就会生成一个app.js.
现在运行试试结果吧。
转:/kf/390.html
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:915193次
积分:10394
积分:10394
排名:第1517名
原创:130篇
转载:227篇
评论:149条
(1)(7)(1)(5)(4)(1)(1)(1)(2)(1)(1)(1)(3)(5)(2)(4)(2)(1)(1)(4)(3)(3)(3)(3)(18)(2)(1)(1)(2)(7)(1)(1)(4)(2)(17)(4)(3)(4)(2)(6)(16)(15)(6)(8)(14)(6)(4)(9)(4)(1)(8)(4)(3)(4)(4)(6)(7)(7)(8)(9)(5)(12)(12)(2)(7)(2)(3)(3)(4)(3)(10)(5)(1)(6)(3)(3)【第3章第358回】基于 TypeScript 的 Node.js 框架 Nest 正式版发布!(上)
本文为译文,原文地址:/nest-final-release-is-here-node-js-framework-built-top-of-type,作者,@Kamil My?liwiec
Nest 是一个强大的 Node.js Web 框架,可以帮助你轻松地构建高效,可扩展的应用程序。它采用现代 Java,基于 Type 构建,并结合了 OOP(面向对象编程)和 FP (功能编程)的最佳概念。
它不仅是又一个框架。你不必等待一个大型的社区,因为 Nest 建立在著名仓库 Express(/expressjs/express)和 socket.io(/socketio/socket.io)之上。这意味着,你可以快速开始使用框架,而不必担心第三方插件的缺失。
Nest 的核心概念是提供一种架构,帮助开发者实现层的最大分离,并且增加了应用程序的抽象。
$ git clone /kamilmysliwiec/nest-type-starter.git projectname
$ cd projectname
$ npm install
$ npm run start
$ npm i --save @nestjs/core @nestjs/common @nestjs/microservices @nestjs/websockets @nestjs/testing reflect-metadata rxjs
设置应用程序
Nest 采用 ES6 和 ES7 (decorators, async / await)功能构建。这意味着,使用它的最简单的方法是 Babel 或 Type。
在本文中,我将使用 Type(它不是必须的!),我推荐大家选择这种方式。示例文件 tsconfig.json 如下:
"compilerOptions": {
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6"
"exclude": [
"node_modules"
记住 emitDecoratorMetadata 和 experimentalDecorators 必须设置为 true。
让我们从头开始我们的应用程序。首先,我们必须被我们的应用程序创建入口模块(app.module.ts ):
import { Module } from '@nestjs/common';
@Module({})
export class ApplicationModule {}
此时,模块的元数据(metadata)为空({}),因为我们只想运行我们的应用程序,并没有加载任何控件或组件。
第二步,创建文件 index.ts,并使用 NestFactory 基于我们的模块类来创建 Nest 应用程序实例。
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';
const app = NestFactory.create(ApplicationModule);
app.listen(3000, () =& console.log('Application is listening on port 3000'));
Express 实例
如果要完全控制 express 实例的生命周期,你可以简单的传递已创建的对象作为 NestFactory.create() 方法的第二个参数,像这样:
import express from 'express';
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './modules/app.module';
const instance = express();
const app = NestFactory.create(ApplicationModule, instance);
app.listen(3000, () =& console.log('Application is listening on port 3000'));
这意味着,你可以直接添加一些自定义配置(例如,设置一些插件,如 morgan 或 body-parser)。
控制器(Controllers)
控制层(Controllers)负责处理传入的 HTTP 请求。在 Nest 中,控制器是一个带有 @Controller() 装饰器的类。
在上一节中,我们为应用程序设置了入口点。现在,让我们来构建我们的第一个文件路径 /users:
import { Controller, Get, Post } from '@nestjs/common';
@Controller()
export class UsersController {
@Get('users')
getAllUsers() {}
@Get('users/:id')
getUser() {}
@Post('users')
addUser() {}
正如你猜想的,我们刚刚创建了一个具有 3 种不同路径的路由:
GET: users
GET: users/:id
POST: users
没有必要重复 users 的每个路径了吧?
Nest 允许我们将额外的元数据传递给 @Controller() 装饰器 - 路径,这是每个路由的前缀。让我们重写我们的控制器:
@Controller('users')
export class UsersController {
getAllUsers(req, res, next) {}
@Get('/:id')
getUser(req, res, next) {}
addUser(req, res, next) {}
正如你看到的, Nest 控制器中的方法和 Express 中的简单路由具有相同的参数列表和行为。
如果你想了解更多关于 req (请求),res(响应)和 next,你可以阅读简短的路由文档。在 Nest 中,它们是等价的。
但是有一个重要的区别。 Nest 提供了一组自定义的装饰器,你可以使用它们来标记参数。
你可以这样使用它们:
@Get('/:id')
public async getUser(@Response() res, @Param('id') id) {
const user = await this.usersService.getUser(id);
res.status(HttpStatus.OK).json(user);
记住在文件的开头导入装饰器。
import { Response, Param } from '@nestjs/common';
UsersController 可以使用,但是我们的模块还不知道。让我们打开 ApplicationModule 并添加一些元数据。
import { Module } from '@nestjs/common';
import { UsersController } from "./users.controller";
controllers: [ UsersController ]
export class ApplicationModule {}
你可以看到,我们只需要将 controller 插入 controllers 数组中,这就够了。
组件(Components)
几乎所有的东西都是组件,Service, Repository, Provider等等。并且他们可以通过构造函数注入控制器或另一组件。
在上一节中, 我们构建了一个简单的 controller,UsersController。这个 controller 可以访问我们的数据(我知道这是一个假数据,但这并不重要)。这不是一个很好的解决方案。我们的控制器只能处理 HTTP 请求,并将更复杂的任务委托给服务(services),这就是为什么我们要创建 UsersService 组件。
实际上,UsersService 应该从持久层调用适当的方法,例如, UsersRepository 组件。我们没有任何类型的数据库,所以我们再次使用假数据。
import { Component } from '@nestjs/common';
import { HttpException } from '@nestjs/core';
@Component()
export class UsersService {
private users = [
{ id: 1, name: "John Doe" },
{ id: 2, name: "Alice Caeiro" },
{ id: 3, name: "Who Knows" },
getAllUsers() {
return Promise.resolve(this.users);
getUser(id: number) {
const user = this.users.find((user) =& user.id === id);
if (!user) {
throw new HttpException("User not found", 404);
return Promise.resolve(user);
addUser(user) {
this.users.push(user);
return Promise.resolve();
Nest 组件是一个简单的类,使用 @Component() 注释。
在 getUser() 方法中可以看到,我们使用了 HttpException。它是 Nest 内置的异常,拥有两个参数,错误消息和状态代码。创建域异常是一个很好的做法,它应该扩展 HttpException (更多见错误处理章节)。
我们的服务准备好了。让我们在 UsersController 中使用它。
@Controller('users')
export class UsersController {
constructor(private usersService: UsersService) {}
getAllUsers(@Response req) {
this.usersService.getAllUsers()
.then((users) =& res.status(HttpStatus.OK).json(users));
@Get('/:id')
getUser(@Response() res, @Param('id') id) {
this.usersService.getUser(+id)
.then((user) =& res.status(HttpStatus.OK).json(user));
addUser(@Response() res, @Body('user') user) {
this.usersService.addUser(req.body.user)
.then((msg) =& res.status(HttpStatus.CREATED).json(msg));
如图所示,UsersService 将被注入到构造函数中。
使用 Type 来管理依赖关系非常简单,因为 Nest 会根据类型识别你的依赖关系。像这样:
constructor(private usersService: UsersService)
这就是你要做的全部。还有一个重要的事是,你必须在 tsconfig.json 中将 emitDecoratorMetadata 选项设置为 true。
如果你不是 Type 爱好者,并且使用纯 Java,则必须按以下方式执行:
import { Dependencies, Controller, Get, Post, Response, Param, Body, HttpStatus } from '@nestjs/common';
@Controller('users')
@Dependencies(UsersService)
export class UsersController {
constructor(usersService) {
this.usersService = usersS
getAllUsers(@Response() res) {
this.usersService.getAllUsers()
.then((users) =& res.status(HttpStatus.OK).json(users));
@Get('/:id')
getUser(@Response() res, @Param('id') id) {
this.usersService.getUser(+id)
.then((user) =& res.status(HttpStatus.OK).json(user));
addUser(@Response() res, @Body('user') user) {
this.usersService.addUser(user)
.then((msg) =& res.status(HttpStatus.CREATED).json(msg));
这很简单,是么?
在这一刻,我们的应用程序甚至还未工作。
为何?因为 Nest 不知道有关 UsersService 的任何内容。此组件不是 ApplicationModule 的一部分,我们必须在那里添加:
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
controllers: [ UsersController ],
components: [ UsersService ],
export class ApplicationModule {}
现在我们的应用程序将执行,但仍然有一个路由不能正常工作,就是 addUser 。为什么?因为我们正在尝试解析请求体(req.body.user),而没有使用 express 的 body-parser 中间件。正如你知道的,可以通过 express 实例作为 NestFactory.create() 方法的第二个参数。
让我们安装插件:
$ npm install --save body-parser
然后在我们的 express 实例中设置它。
import express from 'express';
import * as bodyParser from 'body-parser';
import { NestFactory } from '@nestjs/common';
import { ApplicationModule } from './modules/app.module';
const instance = express();
instance.use(bodyParser.json());
const app = NestFactory.create(ApplicationModule, instance);
app.listen(3000, () =& console.log('Application is listening on port 3000'));
Async / await
Nest 与 ES7 的 async / await 功能兼容。因此我们可以快速重写我们的 UsersController :
@Controller('users')
export class UsersController {
constructor(private usersService: UsersService) {}
async getAllUsers(@Response() res) {
const users = await this.usersService.getAllUsers();
res.status(HttpStatus.OK).json(users);
@Get('/:id')
async getUser(@Response() res, @Param('id') id) {
const user = await this.usersService.getUser(+id);
res.status(HttpStatus.OK).json(user);
async addUser(@Response() res, @Body('user') user) {
const msg = await this.usersService.getUser(user);
res.status(HttpStatus.CREATED).json(msg);
看起来更好么?在这里你可以阅读更多关于 async / await(/type-2-1-introduction-async-await)。
模块(Modules)
模块是一个带有 @Module({}) 装饰器的类。该装饰器提供元数据,该框架用于组织应用程序结构。
现在,这是我们的 ApplicationModule:
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
controllers: [ UsersController ],
components: [ UsersService ],
export class ApplicationModule {}
默认情况下,模块封装每个依赖关系。这意味着不可能在模块之外使用其组件或控制器。
每个模块也可以导入到另一个模块。实际上,你应该将 Nest 模块看做是 模块树。
我们将 UsersController 和 UsersService 移动到 UsersModule。只需创建新文件,例如,users.module.ts 包含以下内容:
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
controllers: [ UsersController ],
components: [ UsersService ],
export class UsersModule {}
然后将 UsersModule 导入到 ApplicationModule (我们的主应用程序):
import { Module } from '@nestjs/common';
import { UsersModule } from './users/users.module';
modules: [ UsersModule ]
export class ApplicationModule {}
这就是全部了。
可以看到出,使用 Nest 可以将代码自然地拆分成可分离和可重用的模块。
模块可以轻松地注入组件,看起来如下:
controllers: [ UsersController ],
components: [ UsersService, ChatGateway ],
export class UsersModule implements NestModule {
constructor(private usersService: UsersService) {}
此外,组件还可以注入模块:
export class UsersController {
constructor(private module: UsersModule) {}
转自: /a/0532
作者: 贝贝
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
如果文章不错,请转发的朋友圈!
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
==========阅读原文==========
责任编辑:
声明:本文由入驻搜狐号的作者撰写,除搜狐官方账号外,观点仅代表作者本人,不代表搜狐立场。

我要回帖

更多关于 青云志2资源 的文章

 

随机推荐