{context}
113 | 114 |requset test ${ctx.params.id}
` 18 | // ctx.body = { success : true} 19 | // ctx.set('Content-Type', 'application/json') 20 | // }) 21 | 22 | 23 | router.get('/a/:id',async (ctx)=> { 24 | const id = ctx.params.id 25 | await handle(ctx.req, ctx.res, { 26 | pathname: '/a', 27 | query: { id } 28 | }) 29 | ctx.respond = false 30 | }) 31 | 32 | server.use(router.routes()) 33 | 34 | server.use(async (ctx, next) =>{ 35 | await handle(ctx.req , ctx.res) 36 | ctx.respond = false 37 | }) 38 | 39 | // server.use(async(ctx, next)=>{ 40 | // const path = ctx.path 41 | // const method = ctx.method 42 | // ctx.body =`koa ${path} ${method}` 43 | // await next() 44 | // }) 45 | 46 | // server.use(router.routes()) 47 | 48 | server.listen(3000,()=>{ 49 | console.log('koa server is starting') 50 | }) 51 | }) 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /store/store.js: -------------------------------------------------------------------------------- 1 | import {createStore, combineReducers, applyMiddleware} from 'redux' 2 | import ReduxThunk from 'redux-thunk' 3 | import { composeWithDevTools } from 'redux-devtools-extension' 4 | const initialState = { 5 | count: 0 6 | } 7 | 8 | const userInitialState = { 9 | name: 'jock', 10 | age : 15 11 | } 12 | 13 | const ADD = 'ADD' 14 | 15 | function countReducer(state = initialState, action){ 16 | console.log(state,action.type) 17 | switch (action.type) { 18 | case ADD: 19 | return { count: state.count + (action.num || 1) } 20 | default : 21 | return state 22 | } 23 | } 24 | 25 | const Update_USERNAME = 'Update_USERNAME' 26 | 27 | function userReducer(state = userInitialState, action){ 28 | switch(action.type){ 29 | case Update_USERNAME: 30 | return { 31 | ...state, 32 | name: action.name, 33 | } 34 | default: 35 | return state 36 | } 37 | } 38 | 39 | 40 | const allReducers = combineReducers({ 41 | counter:countReducer, 42 | user:userReducer 43 | }) 44 | 45 | const store = createStore( 46 | allReducers,{ 47 | counter: initialState, 48 | user: userInitialState 49 | }, 50 | composeWithDevTools(applyMiddleware(ReduxThunk)) 51 | ) 52 | 53 | //action creator 54 | export function add(num) { 55 | return { 56 | type: ADD, 57 | num, 58 | } 59 | } 60 | 61 | //async creator 62 | function addAsync(num) { 63 | return (dispatch) => { 64 | setTimeout(() => { 65 | dispatch(add(num)) 66 | }, 1000); 67 | } 68 | } 69 | 70 | store.dispatch(add(3)) 71 | 72 | console.log(store.getState()) 73 | store.dispatch({type:ADD}) 74 | store.dispatch({ type:Update_USERNAME , name: "wenwenwen" }) 75 | console.log(store.getState()) 76 | 77 | // listen changes of store 78 | store.subscribe(() => { 79 | console.log('change',store.getState()) 80 | }) 81 | 82 | store.dispatch(addAsync(4)) 83 | store.dispatch({ type:Update_USERNAME , name: "ssss" }) 84 | 85 | 86 | 87 | 88 | export default function initializeStore(state) { 89 | const store = createStore( 90 | allReducers, 91 | Object.assign({}, { 92 | counter: initialState, 93 | user: userInitialState 94 | },state), 95 | composeWithDevTools(applyMiddleware(ReduxThunk)) 96 | ) 97 | 98 | return store 99 | } -------------------------------------------------------------------------------- /styles/Home.module.css: -------------------------------------------------------------------------------- 1 | .container { 2 | min-height: 100vh; 3 | padding: 0 0.5rem; 4 | display: flex; 5 | flex-direction: column; 6 | justify-content: center; 7 | align-items: center; 8 | } 9 | 10 | .main { 11 | padding: 5rem 0; 12 | flex: 1; 13 | display: flex; 14 | flex-direction: column; 15 | justify-content: center; 16 | align-items: center; 17 | } 18 | 19 | .footer { 20 | width: 100%; 21 | height: 100px; 22 | border-top: 1px solid #eaeaea; 23 | display: flex; 24 | justify-content: center; 25 | align-items: center; 26 | } 27 | 28 | .footer img { 29 | margin-left: 0.5rem; 30 | } 31 | 32 | .footer a { 33 | display: flex; 34 | justify-content: center; 35 | align-items: center; 36 | } 37 | 38 | .title a { 39 | color: #0070f3; 40 | text-decoration: none; 41 | } 42 | 43 | .title a:hover, 44 | .title a:focus, 45 | .title a:active { 46 | text-decoration: underline; 47 | } 48 | 49 | .title { 50 | margin: 0; 51 | line-height: 1.15; 52 | font-size: 4rem; 53 | } 54 | 55 | .title, 56 | .description { 57 | text-align: center; 58 | } 59 | 60 | .description { 61 | line-height: 1.5; 62 | font-size: 1.5rem; 63 | } 64 | 65 | .code { 66 | background: #fafafa; 67 | border-radius: 5px; 68 | padding: 0.75rem; 69 | font-size: 1.1rem; 70 | font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, 71 | Bitstream Vera Sans Mono, Courier New, monospace; 72 | } 73 | 74 | .grid { 75 | display: flex; 76 | align-items: center; 77 | justify-content: center; 78 | flex-wrap: wrap; 79 | max-width: 800px; 80 | margin-top: 3rem; 81 | } 82 | 83 | .card { 84 | margin: 1rem; 85 | flex-basis: 45%; 86 | padding: 1.5rem; 87 | text-align: left; 88 | color: inherit; 89 | text-decoration: none; 90 | border: 1px solid #eaeaea; 91 | border-radius: 10px; 92 | transition: color 0.15s ease, border-color 0.15s ease; 93 | } 94 | 95 | .card:hover, 96 | .card:focus, 97 | .card:active { 98 | color: #0070f3; 99 | border-color: #0070f3; 100 | } 101 | 102 | .card h3 { 103 | margin: 0 0 1rem 0; 104 | font-size: 1.5rem; 105 | } 106 | 107 | .card p { 108 | margin: 0; 109 | font-size: 1.25rem; 110 | line-height: 1.5; 111 | } 112 | 113 | .logo { 114 | height: 1em; 115 | } 116 | 117 | @media (max-width: 600px) { 118 | .grid { 119 | width: 100%; 120 | flex-direction: column; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /styles/globals.css: -------------------------------------------------------------------------------- 1 | html, 2 | body { 3 | padding: 0; 4 | margin: 0; 5 | font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, 6 | Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; 7 | } 8 | 9 | a { 10 | color: inherit; 11 | text-decoration: none; 12 | } 13 | 14 | * { 15 | box-sizing: border-box; 16 | } 17 | -------------------------------------------------------------------------------- /test/test-redis.js: -------------------------------------------------------------------------------- 1 | //test connect redis 2 | 3 | async function test() { 4 | const Redis = require('ioredis') 5 | 6 | 7 | const redis = new Redis({ 8 | port: 6378, 9 | host:'127.0.0.1', //redis port 10 | // family: 4, // IPv4 or IPv6 11 | password: 123456 12 | }) 13 | // set(key,value) 14 | // setex(key,Expiration-time(second), value) 15 | await redis.setex('c',10,123) 16 | const keys = await redis.keys('*') 17 | console.log(keys) 18 | } 19 | 20 | test() --------------------------------------------------------------------------------