koa-web-kit

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 常用JavaScript包
软件类型 开源软件
地区 不详
投 递 者 羊舌兴德
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

koa-web-kit

�� A Modern, Production-Ready, and Full-Stack Node Web Framework

Release Notes,An Introduction for koa-web-kit

This readme is for v3(require node >= 10.13), if you need SASS/SCSS support, use v2.x

Features

  • Built with all modern frameworks and libs, including Koa, React(like Vue?)...
  • �� Get all the Node.JS full stack development experience out of the box
  • �� Hot Module Replacement support, and bundle size analyzer report
  • �� Async/Await support for writing neat async code
  • �� Great style solutions: Styled-Components, TailwindCSS, CSS Modules
  • �� Simple API Proxy bundled, no complex extra reverse proxy configuration
  • �� Available for generating static site, also with SSR support
  • ⚡️ Just one npm command to deploy your app to production
  • �� Docker support(dev and prod Dockerfile)
  • �� Continuously Maintaining ��

Quick Start

Get the latest version, and go to your project root,Also available on npm.

Before start, copy the config/app-config.js.sample to app-config.js(to project root or config dir) for local dev configuration

  1. Install Dependencies
npm install
  1. Start Dev Server

npm run dev to start koa with HMR enabled, ornpm run dev:ssr to start dev server with SSR enabled(yet HMR will be disabled for now)

  1. Go to http://localhost:3000 to view the default react page

Project Structure

  • __tests__ dir, for your tests
  • mocks dir, for your mock json server and other mock data
  • api dir, the API Proxy utility, also put your api urls in api-config.js for universal import across your app
  • config dir, all webpack build configs are put here, besides, some application-wide env configs getter utilities
  • services dir, some middleware here, default logger utility also located here
  • routes dir, put your koa app routes here
  • src dir, all your front-end assets, react components, modules, etc...
  • utils dir, utilities for both node.js and front-end
  • views dir, your view templates(NOTE: when SSR is enabled, it will use the template literal string)
  • build dir, all built assets for your project, git ignored
  • logs dir, logs are put here by default, git ignored
  • All other files in project root, which indicate their purposes clearly �� .

Application Config and Environment Variables

Every project has some configuration or environment variables to make it run differently in different environments,for koa-web-kit, it also provides different ways to configure your ENVs.

app-config.js/app-config.js.sample

The pre bundled file config/app-config.js.sample lists some common variables to use in the project, you should copy and rename it to app-config.js for your local config, both put it in ${project_root} or the same config dir are supported:

module.exports = {
  //http server listen port
  "PORT": 3000,
  //most commonly used env
  "NODE_ENV": "development",
  //enable/disable built-in API Proxy
  "NODE_PROXY": true,
  //config the api proxy debug level, [0, 1, 2], 0 -> nothing, default: 1 -> simple, 2 -> verbose
  "PROXY_DEBUG_LEVEL": 1,
  //static endpoint, e.g CDN for your static assets
  "STATIC_ENDPOINT": "",
  //add a alternative prefix for your "STATIC_ENDPOINT"
  "STATIC_PREFIX": "",
  //add "/" to the end of your static url, if not existed
  "PREFIX_TRAILING_SLASH": true,
  //global prefix for your routes, e.g http://a.com/prefix/...your app routes,
  //like a github project site
  "APP_PREFIX": "",
  //customize build output dir, default ./build/app
  "OUTPUT_DIR": "",
  //if true, the "/prefix" below will be stripped, otherwise, the full pathname will be used for proxy
  "CUSTOM_API_PREFIX": true,
  //if enable HMR in dev mode, `npm run dev` will automatically enable this
  "ENABLE_HMR": true,
  //if need to enable Server Side Rendering, `npm run dev:ssr` will automatically enable this, HMR need to be disabled for now
  "ENABLE_SSR": false,
  //enable CSS Modules, should disable this when SSR is enabled for now
  "CSS_MODULES": false,
  //simple dynamic routes, based on file structure(like next.js)
  "DYNAMIC_ROUTES": false,
  //single endpoint string, multiple see below, type: <string|object>
  "API_ENDPOINTS": 'http://127.0.0.1:3001',
  //API Proxies for multiple api endpoints with different prefix in router
  "API_ENDPOINTS": {
    //set a default prefix
    "defaultPrefix": "/prefix",
    //e.g http://127.0.0.1:3000/prefix/api/login -->proxy to--> http://127.0.0.1:3001/api/login
    "/prefix": "http://127.0.0.1:3001",
    "/prefix2": "http://127.0.0.1:3002",
  }
}

Environment Variables and Configuration

All the variables in app-config.js can be set with Environment Variables, which have higher priority than app-config.js.e.g:> NODE_ENV=production npm startor

export PORT=3001
export NODE_ENV=production
npm start

You can also use .env file to config envs

Default config.default.[dev|prod].js in config dir

The project comes with default config files just like app-config.js.sample, which will be used if app-config.js above is not provided.

Priority: Environment Variables > .env > app-config.js > config.default.[dev|prod].js

Logs

The builtin services/logger.js provides some default log functionality for your app.By default, the manual log(calling like logger.info()) will be put into ./logs/app.log file,and the http requests will be put into ./logs/requests.log,both will also be logged to console.For more options, checkout the pino.

//use the default logger
const { logger, Logger } = require('../services/logger');
logger.info('message');
logger.error(new Error('test error'));
//create custom logger, log into a different file
const pino = require('pino');
//the 2nd params for the constructor is for only for pino options
const mylogger = new Logger({destination: pino.destination('./logs/my-log.log')}, {});
mylogger.info('my log message');

Production Deployment

Deploy your app to production is extremely simple with only one npm script command, you can provide couple of options for different deployment phases(e.g: install, build, start server),pm2 inside is used as node process manager.

Global installation of PM2 is not required now, we will use the locally installed pm2, but if you want to use pm2 cmd everywhere, you may still want to install it globally

Usage

npm run deploy -- [skipInstall] [skipBuild] [skipServer]The last three options are boolean values in 0(or empty, false) and 1(true).

Examples:

  • npm run deploy: no options provided, defaults to do all the tasks.
  • npm run deploy -- 1: same as npm run deploy:noinstall as an alias, this will skip the npm install --no-shrinkwrap, and just go to build and start server.
  • npm run deploy -- 1 0 1: which will only build your assets
  • npm run deploy -- 1 1 0: which will just start node server, useful when all assets were built on a different machine.

You may need to create/update the deploy.sh to meet your own needs.

Powered By

powered by jetbrains

LICENSE

MIT @ 2016-present jason

  • Vue是一个构建数据驱动的 web 界面的渐进式框架。Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件特别整理了常用的vue插件,来了个大汇总,方便查找使用,便于工作和学习。很全的vue插件汇总,赶紧收藏下吧! 一、UI组件及框架 element - 饿了么出品的Vue2的web UI工具套件 mint-ui - Vue 2的移动UI元素 iview - 基于 V

  • python开发web框架 Having spent the last 20+ years operating mostly in the CMS universe, the time has come for me to see what else the web development ecosystem has to offer, and what has changed in the re

  • 欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:樊东东  前端从业几年,积累了不少github开源库。 有时候想查阅以前star的库,但不好找,github大多库都是英文说明,对中文开发者不太友好,这里整理下收集的github库,方便需要的时候查阅。 其实老早就有整理github上资源的想法,现在才付诸行动。 正在写的时候收到稀土圈公众号开源库功能上线的通知,英雄所见略同。 n

 相关资料
  • Koa-React-Notes-Web This is a simple SPA built using Koa as the backend, Vue as the first frontend, and React as the second frontend. Frontend Vue GitHub Frontend Vue Demo Frontend React GitHub Fronte

  • 先讲 Node.js 基础,然后 Koa 框架(从Koa-generator开始),理解一些基础概念,调试,测试等,然后讲解 http 相关知识,比如 get、post、上传如何实现,如果使用form实现, ajax 实现,如何 koa 实现,如果使用 cli curl 命令测试,如何使用 chrome 的 postman 插件测试,如果使用 supertest 来测试 api。

  • Koa是什么? Koa是Node.js下一代web框架 官方介绍 Expressive HTTP middleware for node.js to make web applications and APIs more enjoyable to write. Koa's middleware stack flows in a stack-like manner, allowing you to

  • Koa

    Koa art-template view render middleware. support all feature of art-template. Install npm install --save art-template npm install --save koa-art-template Example const Koa = require('koa'); const ren

  • koa

    koa是Express的下一代基于Node.js的web框架,目前有1.x和2.0两个版本。 历史 1. Express Express是第一代最流行的web框架,它对Node.js的http进行了封装,用起来如下: var express = require('express'); var app = express(); app.get('/', function (req, res) {

  • Koa

    Koa 是下一代的 Node.js 的 Web 框架。由 Express 团队设计。旨在提供一个更小型、更富有表现力、更可靠的 Web 应用和 API 的开发基础。 Koa可以通过生成器摆脱回调,极大地改进错误处理。Koa核心不绑定任何中间件,但提供了优雅的一组可以快速和愉悦地编写服务器应用的方法。 示例代码: var koa = require('koa');var app = koa();//