koa

nodejs-KOA

和自甴很熟 提交于 2019-12-05 14:39:53
(1)安装KOA 和需要的组件 npm install koa koa-router koa-bodyparser (2)最基本的语句 // 导入koa,和koa 1.x不同,在koa2中,我们导入的是一个class,因此用大写的Koa表示: const Koa = require('koa'); // 创建一个Koa对象表示web app本身: const app = new Koa(); // 对于任何请求,app将调用该异步函数处理请求: app.use(async (ctx, next) => { await next(); ctx.response.type = 'text/html'; ctx.response.body = '<h1>Hello, koa2!</h1>'; }); // 在端口3000监听: app.listen(3000); console.log('app started at port 3000...'); (3)处理router 使用koa-router const Koa = require('koa'); // 注意require('koa-router')返回的是函数: const router = require('koa-router')(); const app = new Koa(); // log request URL:

strapi - restrict user to fetch only data related to him

人走茶凉 提交于 2019-12-05 12:50:04
Usually, a logged-in user gets all entries of a Content Type. I created a "snippets" content type (_id,name,content,users<<->>snippets) <<->> means "has and belongs to many" relation. I created some test users and make a request: curl -H 'Authorization: Bearer eyJ...' http://localhost:1337/snippets/ Main Problem: an authenticated user should only see the entries assigned to him. Instead, a logged-in user gets all snippets, which is bad. How is it possible to modify the fetchAll(ctx.query); query to take that into account so it does something like fetchAll(ctx.state.user.id); at the / -route->

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

北战南征 提交于 2019-12-05 09:06:05
前些日子,在某论坛看到一帖,问「一线城市中小公司的前端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了),现在和react配合着,ts+koa+Graphql作为中间层,react

koa mogoose 创建后台服务连接数据库并进行增删改查

懵懂的女人 提交于 2019-12-05 07:13:17
本文原创 ,转载请标明出处 1 koa的基本使用 koa是基于node平台的web开发框架 使用koa去创建一个服务器 const Koa = require('koa') // bodyParser 处理post请求 const bodyParser = require('koa-bodyparser') const port = 7001 const app = new Koa() app.use(bodyParser()) app.use(async ctx => { ctx.body = 'hello , i am first koa applicance' }) app.listen(port)   koa 使用 下面方式 利用 ctx.body 将内容或接口数据返回给到页面 app.use(async ctx => { ctx.body = '' })   下面一个从后台获取数据返回给到页面 的demo(作为中间层) const Koa = require('koa') // bodyParser 处理post请求 const bodyParser = require('koa-bodyparser') // 用于请求后台数据 当我们想把改服务作为web和后台的中间层时 需要使用request-promise 或者request 当然还有其他的 const

node.js中http模块、koa、pm2、nginx、域名 的关系

孤者浪人 提交于 2019-12-05 04:48:24
如果我们想要自己搭建一个服务器,上面这些名词应该是我们经常会听到的,他们之间有什么关系呢? node.js http模块 基础支持 koa 路由 静态文件 权限 中间件 模板 文件上传 Gzip压缩 pm2 热启动 单机负载均衡 监听 nginx 集群负载均衡 反向代理 虚拟主机 Gzip压缩 域名 服务器于用户之间的连接 来源: https://www.cnblogs.com/mrzhu/p/11907200.html

koa结合mongDB实现后台增删改查

允我心安 提交于 2019-12-05 04:32:46
node.js是一门轻量级后台语言,基于chorme V8引擎的JS运行环境,利用koa框架能够轻易搭建自己本地服务。这里我用koa和mongoDB实现了简单的数据库增删改查。 一、安装依赖 使用npm 或者 yarn 都可以,这里我使用的是yarn 1、yarn add koa //安装koa 2、yarn add koa-router //安装koa-router,轻松实现路由 3、yarn add koa-views //安装koa-views,配置视图模板 4、yarn add koa-body //安装koa-body,用于处理post请求 5、yarn add mongoose //安装mongoose,操作数据库 二、利用koa创建本地服务 koa.js 文件 const Koa = require('koa') const { join } = require('path') const koaBody = require('koa-body') const views = require('koa-views') const router = require('./router') //导入路由模块,下面会有详细代码 const app = new Koa app .use(views(join(__dirname, "views"), {   //配置视图模板

koa2的学习

China☆狼群 提交于 2019-12-05 02:39:49
安装koa的以及koa-router及全局的glob(可使用*号遍历所有文件) npm install koa --save npm install glob --save npm install koa-router --savenpm install koa2-cors --save 引入koa const Koa = require('koa') const app = new Koa() const Router=require('koa-router') const mongoose = require("mongoose") const {connect,initSchemas} = require("./database/init") const cors=require("koa2-cors")//跨域需求 app.use(cors())//注意位置 let entry = require("./appApi/entry.js") let router = new Router(); router.use('/entry',entry.routes())//路由配置项 app.use(router.routes()) //注意 app.use(router.allowedMethods())//注意 ;(async()=>{ await connect()//连接

Context object is empty while receiving POST request , in Koa?

无人久伴 提交于 2019-12-04 20:37:47
I am new in Koa and I am creating a demo app. I want to create an API to handle POST request, But when I console.log(ctx); then there is nothing in ctx in indexRouter.js , the console only printing empty object like {} . I don't know why this is happening. Please anyone suggest me where I am doing wrong? And please help me to get the request.body by POST Method. serverKoa.js: var koa = require('koa'); var router = require('koa-router'); var app = new koa(); var route = router(); //Instantiate the router app.use(route.routes()); //Use the routes defined using the router const index = require('.

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

寵の児 提交于 2019-12-04 18:41:25
问题 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

全栈项目|小书架|服务器开发-Koa2中间件机制洋葱模型了解一下

流过昼夜 提交于 2019-12-04 17:41:57
KOA2 是什么? Koa 是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小、更富有表现力、更健壮的基石。 通过利用 async 函数, Koa 帮你丢弃回调函数,并有力地增强错误处理。 Koa 并没有捆绑任何 中间件 , 而是提供了一套优雅的方法,帮助您快速而愉快地编写服务端应用程序。 为什么产生? 笔者对这几个框架都不熟,这里就不误人子弟了。可以看看下面一些大佬的介绍。 Koa 是由 Express 的原班人马打造,那么他们为什么不将 Express 升级版本呢而是从新开发新的一个项目?看官方的介绍: Koa 与 Express 中间件 中间件(英语: Middleware ),又译中间件、中介层,是提供系统软件和应用软件之间连接的软件,以便于软件各部件之间的沟通,特别是应用软件对于系统软件的集中的逻辑,在现代信息技术应用框架如 Web 服务、面向服务的体系结构等中应用比较广泛。 中间件-维基百科 中间件只是一种服务,没有这种服务系统也能够存在。比如:一台台式机,是由很多个部件组装而成。鼠标、键盘等只是为了让系统更加完善。即使没有鼠标或者键盘,台式机也可以使用其他硬件软件来操作电脑。或者查看 AOP 面向切面编程 中的比喻。 非常有用的 Koa 中间件合集 Koa 是一个中间件框架