GithubHelp home page GithubHelp logo

molunerfinn / vue-koa-demo Goto Github PK

View Code? Open in Web Editor NEW
757.0 24.0 166.0 755 KB

:beginner:A simple full stack demo(CSR & SSR & Docker Support) written by Vue2 & Koa2(Koa1 verson also completed)

License: MIT License

JavaScript 79.16% HTML 0.69% Vue 18.91% Shell 0.24% Dockerfile 1.00%
vue todolist koa koa2 mysql vue-koa-demo vue2 jest jest-tests vue-ssr

vue-koa-demo's Introduction

vue-koa-demo

A fullstack demo used Vue2 & Koa2(Koa1 version is here)

☀️ Easy to setup and learn

💯 Api test coverage

🚀 Instant feedback

😜 Vue SSR support in the ssr branch

🎉 Docker support

Coverage Status

Todolist

View the article for more details.

If you want to check the info of the test, view the article for more details.

Install

git clone https://github.com/Molunerfinn/vue-koa-demo.git

npm install or yarn

if you are using yarn & meet this error:

error [email protected]: The engine "node" is incompatible with this module. Expected version ">=4 <=9".

please use

yarn --ignore-engines

Also you need to install MySQL & create a database named todolist,and execute 2 sql files list.sql & user.sql.They are in sql/

After that, create a .env file and set the database username & password:

# your database username
DB_USER=XXXX
# your database
DB_PASSWORD=YYYY 
# Koa is listening to this port
PORT=8889

If you want to run the test for the Project, please create a .env.test file to face this situation:

# your database username
DB_USER=XXXX
# your database
DB_PASSWORD=YYYY 
# The port which is listened by koa in the test environment
PORT=8888

Run

Node.js & Docker support. You need to create a .env file as above.

Node.js

Beacuse of using Koa2, Node.js >= v7.6.0 is needed.

Development:

npm run dev && npm run server

open browser: localhost:8080

tips: login password is 123

Production:

npm run start

open browser: localhost:8889

tips: login password is 123

Test:

npm run test and find the coverage report in the coverage/lcov/index.html

Docker

docker-compose build && docker-compose up

mysql in docker use 3306 port inside & outside.

open browser: localhost:8889

tips: login password is 123

License

MIT

Copyright (c) 2017 Molunerfinn

vue-koa-demo's People

Contributors

dependabot[bot] avatar gengjiawen avatar molunerfinn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-koa-demo's Issues

你起了两个服务么?

我有点没看懂,
vue-cli里面已经用express起了一个服务,然后你单独用koa又启动了一个服务,是这样么?
我这里有两个问题:

  1. koa的端口你写的是8989,但是vue-cli的默认端口是3000吧,这个会冲突么?
  2. 你是如何在8989端口监听到webpack所编译的文件的呢?就像在vue-cli的dev-server.js中有app.use(devMiddleware)来做这一步。

前端无法引用后端模块jsonwebtoke

你好,我在vue组件中引入jsonwebtoken模块时,报错如下:

ERROR Failed to compile with 2 errors 22:10:55

These dependencies were not found:

  • net in ./~/joi/lib/string.js
  • dns in ./~/isemail/lib/isemail.js

To install them, you can run: npm install --save net dns

Listening at http://localhost:8080

这个要怎么解决,按它的提示安装额外的后端模块也不奏效。。

【单元测试】:全局方法的引入

如果组件a依赖于组件b,组件b依赖于全局的方法(此全局方法为自己封装的),那么如何才能引入此全局方法,完成单元测试呢?
ps:因为客观原因,无法把此全局方法打包出来
尝试的解决方案:
const video = require('../../src/module/tools/index.js')
1 Vue.use(video)
不能成功,因为index.js不是npm包,所以没办法识别
2 把index方法挂载到全局,但是依然没有生效,求教大佬解决方案

ssr index.html的问题

模板index.html的代码

<body>
    <!--vue-ssr-outlet-->
</body>

app.vue的代码:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view></router-view>
  </div>
</template>

entry-client.js代码

router.onReady(() => {
  app.$mount('#app')
})

由于app.vue具有id为app的元素,
所以服务端渲染的时候会返回

<div id="app" data-server-rendered="true">
   <img src="base64.xxxxx"/>
   ....
</div>

网页加载完成后,js开始执行,由于楼主写了app.$mount('#app'), 所以会把服务端渲染的<div id="app" data-server-rendered="true">替换掉,这样的服务端渲染是正确的吗?

感谢博主解答。ssr有点难呀。在踩着坑,哎。

Security issue

Md5 can't be used to encrypt password, you need to use bcrypt or something else.

数据库连接报错

大神,我参照你的写法,出现了如下的数据连接错误,不知道你是否有遇到过
image

想搭建远程调试,一直不成功

git clone后已经成功运行。

想搭建远程调试,一直不成功

server和前端已经在远程服务器运行起来,怎么远程调试?

参考N多文章没有成功,例子都是介绍非常简单的node普通的单例,好像这个demo启动参数有点多

远程服务器 启动调试配置

$ node --inspect

或者

$ node --inspect-brk app.js

可以运行,但链接不上。一直timeout.

$ node --inspect app.js

直接报错,

启动配置

{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

  {
    "type": "node",
    "request": "attach",
    "name": "Attach to Remote",
    "address": "192.168.65.131",
    "port": 9229,
    "localRoot": "${workspaceFolder}",
    "remoteRoot": "/home/test/vue-koa-demo/"
  }
]
}

哪里错了?
服务器:ubuntu 18.

关于开发环境下ssr的问题

package.json文件的代码:

 "server": "nodemon -w app.js -w server server-entry.js"

在命令行输入npm run server后, 运行app.js文件
此时环境为development, 那么会走下面if else 的第二部分代码
博主在这里为何只拿到服务端打包的vue-ssr-server-bundle.json
而没有传入客户端打包的vue-ssr-client-manifest.json

app.js文件的代码:

if (isProd) {
  const template = fs.readFileSync(templatePath, 'utf-8')
  const bundle = require('./dist/vue-ssr-server-bundle.json')
  const clientManifest = require('./dist/vue-ssr-client-manifest.json')
  renderer = createRenderer(bundle, {
    template,
    clientManifest
  })
  app.use(serve(path.resolve('dist'), {
    hidden: 'index.html'
  }))
} else {
  require('./build/setup-dev-server')(app, (bundle, template) => {
    renderer = createRenderer(bundle, { template })
  })
}

项目跑起来了,但是登录接口报504,登录不了 。

$ npm run dev

[email protected] dev C:\Users\Administrator\Desktop\2017.6\vue-koa-demo
node build/dev-server.js

[HPM] Proxy created: /auth -> http://localhost:8889
[HPM] Proxy created: /api -> http://localhost:8889

Starting dev server...
DONE Compiled successfully in 8258ms12:41:49

Listening at http://localhost:8080

[HPM] Error occurred while trying to proxy request /auth/user from localhost:8080 to http://localhost:8889 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

配置了自己的mysql密码,这是什么原因导致的呢。

ssr webpack-hot-middleware error

node -v v11.10.0
npm -v 6.7.0
"webpack-dev-middleware": "^1.12.0",
"webpack-hot-middleware": "^2.18.2"
yarn server or npm run server
f5 刷新頁面
D:***\node_modules\webpack-hot-middleware\middleware.js:104
if (!res.finished) res.end();
^

TypeError: res.end is not a function
at IncomingMessage. (D:\work\dfrc-front\node_modules\webpack-hot-middleware\middleware.js:104:32)
at IncomingMessage.emit (events.js:197:13)
at abortIncoming (_http_server.js:451:9)
at socketOnEnd (_http_server.js:466:5)
at Socket.emit (events.js:202:15)
at endReadableNT (_stream_readable.js:1129:12)
at processTicksAndRejections (internal/process/next_tick.js:76:17)
[nodemon] app crashed - waiting for file changes before starting...

Vue.prototype.$http = axios 报错

您好:
我在编写测试用例的时候这段报错,提示$http只有getter方法,请问您有遇到过这个问题吗?
TypeError: Cannot set property $http of #<Vue$3> which has only a getter

下载项目安装依赖后node app.js报错

报错内容:
D:\study\koa-node0519\vue-koa-demo-master\app.js:18
app.use(async function(ctx, next){
^^^^^
SyntaxError: missing ) after argument list
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

作者大大,方便交流下nginx配置吗?

您好,我已经看过您的文章,但是我疑惑的是server代码是不是要放在服务器里,然后是怎么启动的,每次都要手动的node server_entry.js吗,盼回复,感谢您!

bcrypt.compareSync is not a function

我在实现登录的过程中报错:“bcrypt.compareSync is not a function”。可是我在controllers/users.js引入了bcryptjs,项目依赖中也添加了bcryptjs。
1
2
3

啥时候换成async了,注释没改

注释还是生成器函数,看的有点出戏。
const getUserById = async function(id){ // 注意是function* 而不是function 对于需要yield操作的函数都需要这种generator函数。

The code don't encrypt the password in the client ?

By looking up the network, I have found that, the code don't encrypt the password in the client .


It's confusing for me to understand what you say in your tutorial.

用户在登录页输入账号密码,将账号密码(密码进行md5加密)发送请求给后端

代理端口号出错

➜ vue-koa-demo git:(master) ✗ npm run dev && npm run server

[email protected] dev /Users/XNW/Documents/Vuejs_workspace/vue-koa-demo
node build/dev-server.js

[HPM] Proxy created: /auth -> http://localhost:8889
[HPM] Proxy created: /api -> http://localhost:8889

Starting dev server...

DONE Compiled successfully in 5084ms 17:02:41

Listening at http://localhost:8080

[HPM] Error occurred while trying to proxy request /auth/user from localhost:8080 to http://localhost:8889 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

vscode调试时,项目语法报错

在使用vscode对koa server部分进行调试时,启动报错,

SyntaxError: Unexpected token function
/Users/xiongsheng/personal_project/Github/vue-koa-demo/node_modules/koa-jwt/lib/index.js:17
    const middleware = async function jwt(ctx, next) {
                             ^^^^^^^^
SyntaxError: Unexpected token function

我的launch.json配置如下

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/server-entry.js",
            "sourceMaps": true,
            "runtimeExecutable": null,
            "runtimeArgs": [
            ],
            "outFiles": []
        }
    ]
}

想知道launch.json该如何配置

node:8.2.1 SyntaxError: Unexpected token import

npm start dev is ok, but when I run node app.js, I got these:

 node app.js 
/Users/simon/workspace/studyCode/node/vue-koa-demo/app.js:1
(function (exports, require, module, __filename, __dirname) { import Koa from 'koa';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3

I use nvm to install [email protected], I saw koa2 need version 7.6+.

ssr分支->reademe.md 对.env配置说明有误

如:
image

下面这种是错误的,会被解析为 process.env.DB_USER="XXXX # your database username"

DB_USER=XXXX # your database username

下面这种是正确的,才会被正确解析为 process.env.DB_USER="XXXX"

## your database username
DB_USER=XXXX 

[单元测试] vue-router的跳转怎么验证?

【需求】:期望能够覆盖到页面当中的vue-touter的跳转代码
【现状】:在login.spec.js文件当中没有发现关于vue-router的代码
【问题】:1 如何验证vue-router有效?
2 如何验vue-router执行的正确性?
【解决思路】:1 验证vue-router的push方法是否被调用了
其他的就没有解决思路了,但是不能解决【问题】1,2当中的疑问。
求教大佬。解决思路,能补充点demo最好不过

SSR

Any plans about SSR?

npm run build

npm run build 应该会把koakoa-router 这些后台依赖的包也打包到vendor.js里面吧?

单元测试里面有国际化i18n 报错

大神你好,我参考了你的vue单元测试博客,你的demo没有使用i18n国际化,我的有使用,并且我是这样引入i18n的 import i18n from '@/lang'
Vue.use(Element, {
size: 'medium', // set element-ui default size
i18n: (key, value) => i18n.t(key, value)
})

可是它报错了 信息为 ERROR LOG: TypeError: undefined is not an object (evaluating 'i18n._t')
改成
import i18n from '@/lang'
Vue.use(i18n) 也是报这个错,百忙中求解答

库主是用什么版本的linux系统?安装yarn add sequelize mysql

首先,这个git我可以顺利clone并运行,我想按照教程走一遍,遇到些问题

库主是用什么版本的linux系统?安装yarn add sequelize mysql

按照教程安装完成后,指向序列化数据库生成对应的js老是提示没有安装mysql?

sequelize-auto -o "./schema2" -d todolist -h 127.0.0.1 -u root -p 3306 -x root -e mysql

**Error: Please install mysql package manually**

sequelize-auto -o "./schema2" -d todolist -h 127.0.0.1 -u root -p 3306 -x root-e mysql

我的是ubuntu18.04,mysql版本是

mysql Ver 14.14 Distrib 5.5.60, for Linux (x86_64) using readline 5.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.