koa

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

坚强是说给别人听的谎言 提交于 2019-12-03 15:01:50
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? 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 thing that comes to your mind. Bluebird is also the fastest. So after this version koa should be just using

原生JavaScript进行前后端同构

烈酒焚心 提交于 2019-12-03 13:38:06
什么是前后端同构 明确三个概念:「后端渲染」指传统的 ASP、Java 或 PHP 的渲染机制;「前端渲染」指使用 JS 来渲染页面大部分内容,代表是现在流行的 SPA 单页面应用;「同构渲染」指前后端共用 JS,首次渲染时使用 Node.js 来直出 HTML。一般来说同构渲染是介于前后端中的共有部分。 感觉前端的确是折腾,之前还在流行前后端分离,现在怎么又要做前后端同构了? 原因是现在流行的SPA前端单页面应用比较沉重,首次访问需要加载文件较多,第一次加载过慢,用户需要等待前端进行渲染页面。而且不利于SEO及缓存,并且有一定的开发门槛。 前后端同构通过复用模板和JS文件,让一份代码可以同时跑在服务器和浏览器,首次渲染使用nodejs渲染页面,之后使用SPA路由跳转。可以有效减少用户首次访问的等待时间,并且对SEO比较友好,也便于缓存。 项目简介 本前后端同构项目主要分为两个部分,一个是基于koa2的渲染服务器,另一个是基于原生JS和zepto的前端SPA。 项目的特点是不使用vue和react等框架,门槛低,开发速度快,便于上手,比较轻巧,核心的router部分只有一百行左右的代码。适用于页面交互较少,变化不频繁的场景下,可以有效的提升性能和加载速度。 前端部分 前端部分的核心是路由部分,具体实现可以基于history API或是hash,网上有很多实现,这次主要讲下架构

koa2--04.ejs模板引擎

主宰稳场 提交于 2019-12-03 11:37:09
首先在项目文件下使用cmd,输入:npm install --save koa-views ejs,将koa-views中间件和ejs模板引擎安装到文件中,并自动写入依赖 接在在index.js文件中插入以下代码 const koa = require('koa'); const router = require('koa-router')(); const views = require('koa-views'); var app = new koa(); // views('放置引擎的绝对路径文件夹','对该路径下的每一个ejs文件都渲染成html文件并映射到引擎中'); app.use(views('views',{ map:{ html:'ejs' } })); //示例渲染views文件夹下的index.ejs模板 router.get('/',async(ctx)=>{ await ctx.render('index.ejs'); }); app.use(router.routes()); app.use(router.allowedMethods()); app.listen(3000); 来源: https://www.cnblogs.com/Liqian-Front-End-Engineer/p/11794229.html

Display a static html file with koa.js

北城余情 提交于 2019-12-03 11:24:45
问题 What I want to do is serve the index.html file when the index route (i.e. localhost:3000) is called. I use koa-router for routing, so my route looks like this: app.all("/", function * (next){ //Send the file here }); I tried to use koa-static like this: var serve = require('koa-static'); app.all("/", function * (next){ serve("index.html"); }); But that didn't work. Then I tried to use co-views (I put the html file in the public directory now): var views = require("co-views"); var render =

Can I have koa-static serve assets at a custom (e.g., /static/) path?

℡╲_俬逩灬. 提交于 2019-12-03 11:07:55
The docs at https://github.com/koajs/static and my personal experience trying out koa-static lead me to believe that you can only serve files from the root URL of your app. For example: app.use(serve('./some/dir/')); Given the above use of serve , the URL to access a file ./some/dir/something.txt would be localhost:3000/something.txt . There doesn't seem to be a way to configure my app such that the same file (and all other files in the same dir) is served at localhost:3000/static/something.txt instead. I'm new to Node and to Koa, so I've just begun to dive into this and I'm probably missing

OpenShift not working with certain Nodejs dependencies (Koa)

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I've checked out How to setup KoaJS in Openshift and it's still not working. Here is a part of my package.json file: "engines" : { "node" : ">= 0.12.0" , "npm" : ">= 1.0.0" }, "dependencies" : { "co-busboy" : "^1.3.0" , "forever" : "^0.14.1" , "fs" : "0.0.2" , "koa" : "^0.18.1" , "koa-logger" : "^1.2.2" , "koa-router" : "^4.2.0" , "koa-static" : "^1.4.9" , "path" : "^0.11.14" }, "devDependencies" : {}, "bundleDependencies" : [], "private" : true , "main" : "--harmony app.js" And then to my app.js file. This code works: var http =

“Everything is middleware”

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm learning Koa JS for building the server side part of a small web applications. Watching youtube tutorials and reading guides, I came across the sentence: "Everything is middleware [in Koa]." I have read the wikipedia article about middleware and googled the term, and I have a rough understanding of what middleware is (sort of a middle layer between the very low level stuff and high-level programming). But I don't understand what the claim that "Everything is middleware" means in the context of web applications and NodeJS and why this is

游戏开发经验谈(二):对战类全球服游戏的设计与实现

眉间皱痕 提交于 2019-12-03 07:34:09
上篇文章《游戏开发经验谈(一):游戏架构里隐藏的五个坑及其应对方案》,我们主要讲解了游戏架构设计当中隐藏的一些坑及其应对方案,错过的小伙伴可以回溯之前的内容。本期内容,将会重点介绍对战类全球服游戏的设计思路与技术实现。 对战类游戏的设计思路 协议的选择 游戏设计之初,需要决定选择哪种协议来进行通讯。对于对战类游戏来说,首先推荐的肯定是UDP。 尽管UDP对开发基础有较高的要求,需要开发者自己实现传输成功检验、重传以及可靠性保证等,但相对于低开发成本的TCP,UDP在效率和时效性上都有极大的优势,它可以根据业务特性进行短间隔重传,或者直接发送复数包来提高弱联网状态的成功率。 此外,相比于UDP的低延时,TCP的重传时间是秒级,对于对战类游戏来说,TCP的重传速度无疑会造成非常糟糕的玩家体验,虽然有BBR一类的技术,但也仅仅只能实现更高效的带宽利用,对于实际业务响应效率没有特别大的提升。 当然,除了技术角度分析,公司的实际情况也是需要纳入考虑的重要的一环。总体来说,COC、KOA(阿瓦隆之王)等地图类少交互的游戏用TCP是没有问题的,而CR(皇室战争)、自由之战、全民枪战、枪林弹雨,这类MOBA、FPS等强联网需求的对战游戏,UDP基本是必备的。 同步机制 帧同步: 快节奏、同时希望降低服务器不必要负载的对战类手游,帧同步是不二的选择。帧同步的好处是可以精减上传信息

Koa.js - serving static files and REST API

泪湿孤枕 提交于 2019-12-03 07:20:30
I'm new to koa.js library and I need some help. I'm trying to make simple REST application using koa. I have a static html and javascript files I want to serve on route / and REST API accessing from /api/ . This is my project directory tree: project ├── server │ ├── node_modules │ ├── package.json │ └── src │ ├── config │ ├── resources │ └── server.js ├── ui │ ├── app │ ├── bower.json │ ├── bower_components │ ├── dist │ ├── node_modules │ ├── package.json │ └── test This is my source: var app = require('koa')(); app.use(mount('/api/places', require('../resources/places'))); // does not work

What are the differences between Koa and Express 4.0?

大兔子大兔子 提交于 2019-12-03 07:11:52
问题 Koa and Express 4.0 are both fairly new, and from what I've read, Koa was made by the Express team. From what I understand, Koa requires features of node that are only available in 0.11 (the unstable branch) of node, and also uses generators. Express 4.0 seems to only be the next version of the Express framework. Are there any differences I am missing completely? Is it likely (based on what the Express team has publicly stated) that Koa and Express will merge at some point in the future?