koa

accessing variable globally in marko template

拜拜、爱过 提交于 2019-12-04 16:07:15
We are using marko template engine in nodejs application. We have 3 marko layout header.marko layout.marko footer.marko header and footer layout render inside layout.marko when ever we create a new marko page (content page) we use layout marko like this <layout-use template="./../layout.marko"> and load marko like this this.body = marko.load("./views/home.marko").stream(data); Now we want to access a variably globally. i-e if we have a variable username='abc'. we want to access and display this name in header , layout or footer marko file. But we do not want to pass username for each content

Koa.js - serving static files and REST API

二次信任 提交于 2019-12-04 11:28:31
问题 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

How to use socket.io together with webpack-hot-middleware?

て烟熏妆下的殇ゞ 提交于 2019-12-04 08:58:49
问题 I have a Koa server using webpack-dev-middleware and webpack-hot-middleware doing hot module replacement (HMR), so the middleware uses a websocket to push changes to the client. But my application code also needs its own websocket connection between the client and the Koa server. I have no idea how to achieve that? Seems like the two are conflicting. Can I have them side by side? My server code looks something like this const compiler = webpack(webpackConfig) const app = new Koa() app.use

koa-router中间件生态系统

无人久伴 提交于 2019-12-04 06:55:26
说下koa-router中间件生态系统:koa-router文档地址 https://www.npmjs.com/package/koa-router 1、强大的koa中间件生态系统,提供了中间件koa-router来完成路由的配置2、Koa中的路由和Express不同,Express是把路由集成在Express中,Koa则需要通过kao-router模块使用 安装koa:cnpm install koa --save 安装koa-router:cnpm install koa-router --save 使用: 路由导航 get请求获取参数 (ctx.query) 动态路由及其获取参数(/product/:id ctx.params.id) 引入 koa模块 //引入 koa模块 var Koa=require('koa'); var router = require('koa-router')(); /*引入是实例化路由** 推荐*/ //实例化 var app=new Koa(); router.get('/',async (ctx)=>{ ctx.body="首页"; }) app.use(router.routes()); /*启动路由*/ app.use(router.allowedMethods()); /* * router.allowedMethods()作用:

koa2 ctx.status 返回值的问题

岁酱吖の 提交于 2019-12-04 06:16:52
在定义koa的路由时,统一处理404,发现在ctx.response之前的status为404,之后的status为200 //koa-router const index = async (ctx, next) => { ctx.response.type = 'html'; ctx.response.body = await readPage('index.html'); } const home = async (ctx, next) => { console.log(ctx.status);//打印404 ctx.response.type = 'html'; ctx.response.body = await readPage('home.html'); console.log(ctx.status);//打印200 } router.get('/', index); router.get('/index', index); router.get('/home', home); //使用中间件 处理404 app.use(async (ctx, next) => { await next(); if(ctx.status === 404 ){ ctx.response.type = 'html'; ctx.response.body = await readPage(

How to run simple app with koa2?

雨燕双飞 提交于 2019-12-04 04:53:32
问题 Problem I am trying to run simple http server using koa2, but have problems running it. It uses es6 that is expected to work in future node.js versions and I was wondering how can I run it with node v6.1.0 ? Code import Koa from 'koa'; const app = new Koa(); // Setup handler. app.use(async ctx => { ctx.body = "Hello World!"; }); // Start server. app.listen(3000); Output $ node --version v6.1.0 $ node --harmony index.js C:\Users\gevor\WebstormProjects\untitled1\index.js:1 (function (exports,

TypeScript 3 + Koajs + Node.js

亡梦爱人 提交于 2019-12-03 23:56:48
转发 TypeScript 3 + Koajs + Node.js 自NodeJS早期以来,Express一直是NodeJS开发人员事实上的标准Web框架。 但是,JavaScript在过去几年中已经走过了漫长的道路,像promises和async函数这样的功能使得构建更小,更强大的Web框架成为可能。 Koa就是这样一个框架。 它由Express背后的团队构建,以利用最新的JavaScript和NodeJS功能,特别是异步功能。 与Express和其他node框架(如Hapi)不同,Koa不需要使用回调。 这消除了难以跟踪的错误的巨大潜在来源,并使框架非常容易为新开发人员选择。 在本文中,我将向您介绍如何使用Koa和TypeScript来开发新的Web应用程序项目 第一步、安装和配置 Koa需要一个具有异步功能支持的Node版本,因此在开始之前确保安装了Node 8.x(或更高版本)。 Node 8将于2017年10月成为新的长期支持版本,因此它是启动新项目的绝佳选择。 我们现在将创建一个安装了以下内容的新node项目: 1. Koa 2. Koa Router 3. TypeScript 4. TS-Node 和 Nodemon(用于在开发期间自动构建和重启) 为项目创建一个新文件夹,然后执行以下命令: npm init # and follow the resulting

koa2--session的实现

大城市里の小女人 提交于 2019-12-03 20:31:52
koa2原生功能只提供了cookie的操作,但是没有提供session操作。session只能自己实现或者通过第三方中间件实现。 如果session数据量很小,可以直接存在内存中 如果session数据量很大,则需要存储介质存放session数据 数据库存储方案 将session存放在Redis数据库中 需要用到中间件 koa-generic-session koa2 处理session的中间件,提供存储介质的读写接口 。 koa-redis 为koa-generic-session中间件提供Redis数据库的session数据读写操作。 将sessionId和对应的数据存到数据库 将数据库的存储的sessionId存到页面的cookie中 根据cookie的sessionId去获取对应的session信息 Redis安装: https://www.runoob.com/redis/redis-install.html 安装成功后,进入redis根目录,运行: redis-server redis.windows.conf 成功显示: 依赖包安装: npm install koa-generic-session koa-redis 使用演示: 1 const Koa = require('koa') 2 const redisStore = require('koa-redis')

node框架

可紊 提交于 2019-12-03 16:52:26
nodejs的框架 最近来node的火热,带动了一大批的框架,例如 express koa sails loopback thinkjs egg 这些是我比较过的框架,下面依次做个简单介绍 express 这个是使用最多的框架,也是各个推荐新手入门的框架。 Express 不对 Node.js 已有的特性进行二次抽象,只是在它之上扩展了 Web 应用所需的基本功能(个人感觉相当于node中的jquery) 封装了路由 静态资源托管 中间件的概念 内置了jade,ejs模板引擎 个人评价,express适合小型项目,不适合大型企业级项目,个人用用还可以,做为快速入门是个很好的选择,用过之后就可以考虑进入 koa 框架的道路 koa2 koa 是比 express 思想更先进的框架,是express原班人马打造 koa解决的最大问题,利用async await的新语法特性,解决回调地狱的问题 koa 与 express 最大的不同,个人觉得有3点: 1.在于 handler 的处理方法,express 是普通的回调函数, koa 是利用ES7 中 Async/Await 的特性,没有回调,没有回调,就大大加速了开发速度这一点而言,已经足以让我们跪舔了 2.koa是洋葱中间件模式,执行到next的时候,会去调用下一个中间件,下个中间件执行完再接着执行上个中间件next下面的代码 3

KOA草稿

不问归期 提交于 2019-12-03 15:42:14
koa-static cnpm i koa-static -d const static = requiere('koa-static') 新建static文件夹 static文件夹下面新建1.html server.js加上 server.use(static('./static')); 即可 let staticRouter = new Router(); server.use(static('./static'),{ maxage:864000*1000, //缓存时间,可降低服务器压力 index: '1.html' //默认文件 }) staticRouter.all(/(\.jpg|\.png|\.gif)/i,static('./static',{ maxage:60*86400*1000 })) //如果是jpg,png,gif文件那就缓存两个月 staticRouter.all(/(\.css)$/i,static('./static',{ maxage:1*86400*1000 })); //如果是css文件那就缓存一天 staticRouter.all(/(\.html|\.htm|\.shtml)$/i,static('./static',{ maxage:20*86400*1000 })); staticRouter.all('',static('.