Koavue-nodejs指令备忘录

NodeJs框架 Koa

2019-06-06  本文已影响0人  Grayly吖

目录

一、\color{red}{Koa} 简介

文档地址:https://koa.bootcss.com

      \color{red}{Koa} 是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小、更富有表现力、更健壮的基石。 通过利用 async 函数,Koa 帮你丢弃回调函数,并有力地增强错误处理。 Koa 并没有捆绑任何中间件, 而是提供了一套优雅的方法,帮助您快速而愉快地编写服务端应用程序。

二、\color{red}{Koa} 之hello world

      npm init -y
      npm i koa -S
    // 导入koa模块
    const Koa = require('koa');
    // 创建koa的实例app
    const app = new Koa();

    app.use(async ctx => {
        ctx.body = "Hello World"
    })
    // 监听端口
    app.listen(3000, () => {
        console.log("服务器已启动,http://localhost:3000");
    })
    node app

三、服务器自动重新部署

   npm i nodemon -g
    nodemon app

四、Koa中间件

    app.use((ctx, next) => {
        // 在ctx上放入username,后面的所有请求的ctx里都会有username这个变量
        ctx.username = '我是Grayly';
        // 处理完之后放行,不使用next()的话,程序会被挂起来不动了
        next();
    })

五、Koa路由配置

    npm i koa-router -D
    // 导入koa-router模块
    const Router = require('koa-router');
    // 创建koa-router的实例router
    const router = new Router();
    router.get('/', ctx => {
        ctx.body = "哈哈哈"
    })
    app.use(router.routes());

六、设置静态目录\color{red}{koa-static}

    npm i koa-static -D
    const koaStatic = require('koa-static');
    app.use(koaStatic(__dirname + '/public'));

七、使用模板引擎\color{red}{koa-views}

    npm i koa-views -D
    npm i underscore -D
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <h3><%=title %></h3>
    </body>
    </html>
  const path = require('path');
  const views = require("koa-views");
  app.use(views(path.join(__dirname, "views"), {
    map: {
      html: 'underscore'
    }
  }));
    router.get("/html", async ctx => {
        await ctx.render("tpl", {
            title: "Grayly"
        })
    })

八、Vue SSR(vue服务器渲染)

  npm install vue vue-server-renderer -S
    router.get('/ssr', async ctx => {
        第 1 步:创建一个 Vue 实例
        const Vue = require('vue')
        const app = new Vue({
            template: `<div>Hello World</div>`
        })
        第 2 步:创建一个 renderer
        const renderer = require('vue-server-renderer').createRenderer();
        第 3 步:将 Vue 实例渲染为 HTML
        renderer.renderToString(app, (err, html) => {
            if (err) throw err
            console.log(html)
            ctx.body = html;
            // => <div data-server-rendered="true">Hello World</div>
        })
    })

九、\color{red}{Koa} 跨域设置

    app.use((ctx, next) => {
    // 设置允许跨域
        ctx.set("Access-Control-Allow-Origin", "*");
        ctx.set("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS");
        // 请求头设置
        ctx.set(
            "Access-Control-Allow-Headers",
            `Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild,x-token,sessionToken,token`
        );
        if (ctx.method == "OPTIONS") {
            ctx.body = 200;
        } else {
            next();
        }
    })
Koa
上一篇下一篇

猜你喜欢

热点阅读