koa

01-koa2基础

眉间皱痕 提交于 2019-12-11 17:07:04
1、安装脚手架 npm install -g koa-generator 2、创建目录 koa2 -e koa2-learn 3、cd koa2-learn 4、npm install 5、SET DEBUG=koa* & npm start koa2-learn 看到 > node bin/www 表示已经成功运行 localhost:3000 测试访问 也可以使用 npm run dev 来启动koa2 来源: https://www.cnblogs.com/suni1024/p/12023600.html

nodejs(koa):Can't set headers after they are sent

两盒软妹~` 提交于 2019-12-11 14:32:00
问题 I have a program which want to map /a/b/c.js url => /a:b:c.js file; koa version:2.3.0 koa static version: 4.0.1 minimal reproduction const KOA = require('koa'); const koaStatic = require('koa-static'); staticApp = new KOA() staticApp.use((ctx, next) => { let path = ctx.path.split('/'); path = path.filter(segment => segment) ctx.path = `/${path.join(':')}`; next() }) staticApp.use(koaStatic(__dirname)) staticApp.listen(8888); Assume current directory have a file a:b:c.js , when I access to

How do I access libraries I'm adding via require, in my Node/Koa server

人走茶凉 提交于 2019-12-11 14:15:37
问题 I'm trying to require an external library in my Node app (Koa server). I'm adding njwt in my main server.js file var njwt = require('njwt'); But I can't access njwt , in my route handler function it gives an error saying njwt is undefined. From this answer (https://stackoverflow.com/a/5809968), it seems that using strict mode in my main server.js file makes functions and variables defined in my imported file inaccessible. But what's the workaround? 回答1: If I am understanding correctly, all

How to setup KoaJS in Openshift

与世无争的帅哥 提交于 2019-12-11 11:48:25
问题 I've been following the example on how to enable custom node version in openshif (http://www.zev23.com/2014/04/openshift-harmony-nodejs.html) so when I ssh to the application I can see the node version(0.11.14) installed but the when I look at the logs it says app.use(function *(){ ... SyntaxError: Unexpected token * and I'm getting a 503 error. here's my server.js file: var koa = require('koa'); var app = module.exports = koa(); app.use(function *(){ this.body = 'Hello World'; }); var

How to create a Promise with default catch and then handlers

谁说我不能喝 提交于 2019-12-11 04:45:42
问题 I'm creating an API that is using koa and babel async/await Every promise in my controllers function looks like this: async function ... { await Promise ... .then(data => response function) .catch(err => err function) } Each promise has the exact same response and error function. Is there a way for me to automatically have each promise resolve with the same then/catch (like a default resolve function for the promise). Then my code would look like this: async function ... { await Promise ... }

Firebase functions: koa.js server how to deploy

情到浓时终转凉″ 提交于 2019-12-11 00:27:47
问题 I already have an app written in MERN stack with koa server prepared build version. My main node file to run by node server.js command to start the whole app looks like this. In every tutorial, I see that I need to add functions.https.request etc. in the beginning of coding (or at least to suppose doing it). How could I host my app on firebase the same as I could on heroku - with whole server side? 回答1: You can't deploy and run an arbitrary node app on Cloud Functions. You have to make use of

Can't remove headers after they are sent

独自空忆成欢 提交于 2019-12-10 23:37:02
问题 I am getting inconsistent results from my server. Sometimes the right response is sent and sometimes I get the error Can't remove headers after they are sent Using Node.js, Koa.js, and Mongoose router .get('/events/', function* getEvent() { let eventList = []; yield Event.find({}, (error, events) => { if (error) { this.response.body = 'Unable to get events.'; this.status = 404; return; } eventList = events; eventList.sort((first, second) => { // sort implementation }); this.response.body =

What is the use of a session store?

大城市里の小女人 提交于 2019-12-10 23:08:53
问题 I am using koa and passport therefore I am using the koa-generic-session middleware to handle my sessions. Now unlike the expressjs session middleware there isn't a big disclaimer in the documentation like such : The default server-side session storage, MemoryStore, is purposely not designed for a production environment. Assuming the MemoryStore for the koa-generic-session is ready for production use, why would I use a heavy duty session store such as mysql or mongodb for sessions that come

Error handling with promises in Koa

試著忘記壹切 提交于 2019-12-10 10:18:30
问题 If I yield promises in Koa, they may be rejected: function fetch = (){ var deferred = q.defer(); //Some async action which calls deferred.reject(); return deferred.promise; } this.body = yield fetch(); //bad, not going to work Is there a error-handling pattern in Koa to handle this besides explicitly unwrapping the promise with then and handling the error explicitly? 回答1: Try/Catch. Under the hood koajs uses co. Check out the docs for co, which describes error handling a little better.

koa入门第一课:通过不同路由(url)及方法(method)返回不同结果(response)

*爱你&永不变心* 提交于 2019-12-10 01:17:44
const Koa = require('koa'); const app = new Koa(); app.use(async (ctx, next) => { if (ctx.url === '/') { ctx.body = '这是主页'; } else if (ctx.url === '/users') { if (ctx.method === 'GET') { ctx.body = '这是用户列表页'; } else if (ctx.method === 'POST') { ctx.body = '创建用户'; } else { ctx.status = 405; } } else { ctx.status = 404; } }); app.listen(3000); console.log('服务开启成功,请在打开浏览器运行:', 'http://localhost:3000'); 来源: https://www.cnblogs.com/lihao97/p/12013841.html