koa

ajax与spring mvc请求的总结

谁都会走 提交于 2019-12-07 01:06:22
https://www.cnblogs.com/love540376/p/6912637.html dataType分类 dataType 预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如XML MIME类型就被识别为XML。 在1.4中,JSON就会生成一个JavaScript对象,而script则会执行这个脚本。随后服务器端返回的数据会根据这个值解析后,传递给回调函数。 可用值: "xml": 返回 XML 文档,可用 jQuery 处理。 "html": 返回纯文本 HTML 信息;包含的script标签会在插入dom时执行。 "script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了"cache"参数。'''注意:'''在远程请求时(不在同一个域下),所有POST请求都将转为GET请求。(因为将使用DOM的script标签来加载) "json": 返回 JSON 数据 。 "jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。 "text": 返回纯文本字符串 contentType contentType 是指http/https发送信息至服务器时的 内容编码类型

Node.js Web开发(二)

*爱你&永不变心* 提交于 2019-12-06 22:50:33
上一篇文章主要介绍了koa2的基本操作(坐下坐下),但是每次都返回一样的HTML似乎也不可能,所以现在我们需要来处理URL了。 直接来点高级点的东西,我们需要一个能处理URL的middleware,它叫做: koa-router : npm install koa-router 1234567891011121314151617181920 const Koa = require('koa');const router = require('koa-router')();const app = Koa();router.get('/hello/:name', async(ctx, next) => { var name = ctx.params.name; ctx.response.body = `<h1>Hello, {$name}!</h1>`;});router.get('/', async(ctx, next) => { ctx.response.body = '<h1>Index Page</h1>';});app.use(router.routes());app.listen(3000);console.log('Server listen on port 3000...'); 这里的代码,会在访问网址localhost:3000时返回Index Page

vue 解决跨域 node服务搭建详细步骤

喜欢而已 提交于 2019-12-06 15:03:16
当前项目同级别新建文件夹 然后进入服务文件夹 npm init 然后一顿回车 自己创建JS app.js npm install -s koa 安装koa2 npm install koa-body -s 安装koa-body(接收post请求) 启动服务 node app.js 源码: const Koa = require('koa'); const app = new Koa(); const axios = require('axios'); const koaBody = require('koa-body'); app.use(koaBody()) app.use(async ctx => { console.log(ctx.request.body) axios({ withCredentials: true, method: 'post', url: '', data: { phone:ctx.request.body.phone } }) .then(function(response) { console.log(response) ctx.body = response }) }); app.listen(3000,function(){console.log('启动')}); 启动完成 本地代码url填写服务端口号:127.0.0.1:3000

accessing variable globally in marko template

好久不见. 提交于 2019-12-06 09:08:07
问题 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

Koa2: how to write chain of middleware?

。_饼干妹妹 提交于 2019-12-06 07:28:53
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 ! If you're simply interested in making sure a middlware runs for every route, all you have to do is register the middleware before you register your routing middelware. app.use

Mount Koajs app on top of Express

筅森魡賤 提交于 2019-12-06 03:55:09
From koajs.com: app.callback() Return a callback function suitable for the http.createServer() method to handle a request. You may also use this callback function to mount your koa app in a Connect/Express app. Now I have an Express app that already starts its own http server. How can I mount a koa app on top of this existing server, so that it shares the same port? Would I include the koa app as an Express middlware? Do I still use app.callback() for that? expressapp.use(koaapp.callback()) is fine. but remember, koaapp.callback() does not have a next , so there's no passing errors to the

“Everything is middleware”

心不动则不痛 提交于 2019-12-06 00:44:56
问题 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

Error handling with promises in Koa

老子叫甜甜 提交于 2019-12-05 21:24:47
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? Try/Catch. Under the hood koajs uses co . Check out the docs for co, which describes error handling a little better. function fetch = (){ var deferred = q.defer(); //Some async action which calls deferred.reject(); return deferred

十大 Node.js 的 Web 框架

和自甴很熟 提交于 2019-12-05 15:27:57
1、Node.js 开发框架 Sail.js Sails.js 就像是 Node.js 平台上的 Rails 框架。这是一个可靠可伸缩的开发框架,面向服务的架构,提供数据驱动的 API 集合。用来开发多玩家游戏、聊天应用和实时面板引用非常方便,也可用于开发企业级 Node.js 应用。 Sails.js 基于 Node.js , Connect , Express 和 Socket.io 构建。 控制器示例代码: module .exports = { hi: function ( req, res ) { return res.send( "Hi there!" ); }, bye: function ( req, res ) { return res.redirect( "http://www.OSChina.net" ); } }; 2、Node.js 服务器端框架 Hapi.js Hapi.js 是一个用来构建基于 Node.js 的应用和服务的富框架,使得开发者把重点放在便携可重用的应用逻辑而不是构建架构。内建输入验证、缓存、认证和其他 Web 应用开发常用的功能。 示例代码: var Hapi = require ( 'hapi' ); // Create a server with a host and port var server = new Hapi

nodejs资料

孤街醉人 提交于 2019-12-05 15:24:34
1 ES6/7语法讲解 http://es6.ruanyifeng.com/#README https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects JavaScript标准库介绍(火狐官方) JavaScript作用域,this,执行上下文,闭包介绍(基本可以看懂) https://www.cnblogs.com/wangfupeng1988/p/3977924.html ES5解决异步嵌套:async模块 https://caolan.github.io/async/docs.html#eachOfSeries ES6/7解决异步嵌套:async funtion+await+promise Promise库-bluebird: http://bluebirdjs.com/docs/why-performance.html 2 koa框架入门 http://www.jianshu.com/p/6b816c609669 https://github.com/ChenShenhai/koa2-note/ https://koa.bootcss.com/ 3 balel关于新语法项目在旧解释器上的支持 ES6项目在ES5解释器(低版本的node环境) https://babeljs