koa

使用typescript来写koa

孤人 提交于 2020-01-13 06:46:30
本文基于win7:安装好必要的nodejs环境后做下面的事情 1,在任意你喜欢的路径下创建一个空文件夹 2,打开这个空文件夹并在空白处shift+鼠标右键 => "在此处打开命令窗口" => 输入 npm init。一路回车。 3,输入 npm install koa,回车 4,输入 npm install @types/koa,回车 5,输入 tsc --init,回车。此时,可以看到当前目录下面有一个 tsconfig.json 文件。 6,打开 tsconfig.json,将 "target" 字段的值改为 ES2017。将"esModuleInterop"的值改为false或者直接注释掉(这步非常重要!) 7,建立一个名字叫做 index.ts 的文件,将下面代码无脑复制 import * as Koa from "koa"; const app = new Koa(); app.use(ctx => { ctx.body = "hello koa2"; }); app.listen(3000); 8,如果你用vscode,请使用ctrl+shift+b进行编译,然后f5运行。如果不用这个,自行编译index.ts然后运行index.js文件。 9,打开浏览器,输入 127.0.0.1:3000,回车。就可以看到 hello koa2 字样了。 进阶文章

Get user id socket.io, passport, koa

一世执手 提交于 2020-01-12 05:46:47
问题 I'm using Koa, Passport.js and koa-session to authenticate users. So it basically looks like: // session var session = require('koa-session'); app.keys = [config.secret]; app.use(session()); // auth require(__dirname+'/lib/auth'); // de/serializeUser, strategies etc.. var passport = require('koa-passport'); app.use(passport.initialize()); app.use(passport.session()); This works well. On requests I do have the req.user , with the user id. But when using sockets, I can do: io.on('connection',

How can I configure WebStorm to provide code completion for KoaJS?

爷,独闯天下 提交于 2020-01-11 08:46:31
问题 Currently, WebStorm reports that KoaJS's methods are undefined. It's a minor, yet persistent annoyance. I've searched on the net and I've searched through WebStorm's configuration dialogs to no avail. Does anyone use WebStorm with KoaJS and have intellisense/code completion working properly? 回答1: It's a known bug, please follow WEB-11299 for updates 回答2: Doing either File > Settings > Languages & Frameworks > JavaScript > Libraries or Click the face in the bottom right corner of the IDE >

What's the right way to set routing for angular2 with koajs

北城余情 提交于 2020-01-07 03:15:30
问题 I'm a newbie to angular2 and koajs. Maybe it's a silly question. I have an angular2 app with a routing table as below: const appRoutes: Routes = [ { path: 'pats/report/settings', component: PatsReportSettingsComponent }, { path: 'pats/report/:id', component: PatsReportComponent, pathMatch: 'full' }, { path: 'pats/report/login', component: PatsLoginComponent, pathMatch: 'full' }, { path: '.', redirectTo: 'pats/report/login', pathMatch: 'full' }, { path: '**', component: Pats404PageComponent,

10k前端工程师应该会什么?

安稳与你 提交于 2020-01-06 14:08:57
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前些日子,在某论坛看到一帖,问「一线城市中小公司的前端10k应该会什么?」,下面有很多回答,大多数回答的是一些会前端三大基础啊,html,css,JavaScript这些,然后一两个框架。实在是太笼统了,这回做一个总结,写下来,也让更多对编程,对前端感兴趣,并想以此为饭碗的朋友们多了解一下! 以下是匿名用户的回答: 毕业一年,工资9.5k,加上补贴奖励, 税后10.5k左右 。 1. html、css、js基础自我感觉良好。语义化、写点css3动画、写写原生js等。 2. 在学校期间就学了前端用jq、bootstrap做过一些项目,学了vue基础,能简单搭个vue项目。 3. webpack只能说会用,一般直接配置有些api记得不清楚,需找文档,主要是不用常配,基本项目开头配置一次,中期优化配置。 4. vue前后做了2个商业项目(公司主要用的vue+elementUI)。 5. react未做过项目,自己找些网站模仿着写点东西(没机会用react做项目)。 6. 代码基本用上ES6,还有async/await等一些语法。 7. 略懂一些常见简单的优化,js优化、懒加载等等。 8. node的话看过视频跟着做了一个小项目,那时候还学了点mongodb、jade等(不过好久没用,忘记7788了)

How to set http status code in GraphQL

末鹿安然 提交于 2020-01-04 05:38:09
问题 I want to set an http status code in my GraphQL authentication query, depending on if auth attempt was successful (200), unauthorised (401) or missing parameters (422). I am using Koa and Apollo and have configured my server like so: const graphqlKoaMiddleware = graphqlKoa(ctx => { return ({ schema, formatError: (err) => ({ message: err.message, status: err.status }), context: { stationConnector: new StationConnector(), passengerTypeConnector: new PassengerTypeConnector(), authConnector: new

Running code AFTER the response has been sent by Koa

亡梦爱人 提交于 2020-01-04 04:30:28
问题 To optimize the response delay, it is necessary to perform work after are response has been sent back to the client. However, the only way I can seem to get code to run after the response is sent is by using setTimeout . Is there a better way? Perhaps somewhere to plug in code after the response is sent, or somewhere to run code asynchronously? Here's some code. koa = require 'koa' router = require 'koa-router' app = koa() # routing app.use router app app .get '/mypath', (next) -> # ...

Unable to catch knex transaction rejection

允我心安 提交于 2020-01-03 07:05:09
问题 I'm using knex transaction with async/await syntax as suggested in this question: Get Knex.js transactions working with ES7 async/await My problem is, that when transaction fails and trx callback is invoked, knex logs Unhandled rejection error: relation "some_table" doesn't exist // Example error which I used for testing just under the same error logged by logger, so logs looks like that: // Removed error stacks... // Error logged by logger 2019-07-14T23:12:29.606Z [error]: error: insert into

Koa2: how to write chain of middleware?

半世苍凉 提交于 2020-01-02 10:04:42
问题 So in express, we can have a chain of middleware, copies an example: middleware = function(req, res){ res.send('GET request to homepage'); }); app.get('/', middleware, function (req, res) { res.send('GET request to homepage'); }); What's the equivalent way to write this in koa2 please ? I'm thinking of using it for the route, for each route i want to have a middleware to check if user is already logged in. Thanks ! 回答1: If you're simply interested in making sure a middlware runs for every

Co.js and bluebird.js — what's the difference?

梦想与她 提交于 2020-01-01 05:16:31
问题 Could someone help me understand the differences between using Koa.js and Bluebird.js with ES6 Harmony. Specifically, how does co( function * () { //stuff } ); compare to, Promise.coroutine( function * () { //stuff } ); It just seems Koa should be using Bluebird and not recreating the wheel. What's different? 回答1: For now the difference is that Koa allows yielding more than just promises. However a feature is being added that allows not only yielding callbacks, thunks etc but any arbitrary