最近一些工作涉及到服务器的知识,之前搭建服务器使用的是hush-framework,好久没用,最近感觉egg框架比较简单流行,这次为了快速,选择egg框架,官方将成已经说的很清楚了,但是比较多,所以简化一下,记录下来。
官方地址教程:https://eggjs.org/zh-cn/intro/quickstart.html
步骤一:直接使用快速初始化(这时目录中应该有一个空的app目录)
$ mkdir egg-example && cd egg-example
$ npm init egg --type=simple
$ npm i
步骤二:增加scripts
添加 npm scripts 到 package.json:
{
"name": "egg-example",
"scripts": {
"dev": "egg-bin dev"
}
}
步骤三:增加Controller(这个是真正执行代码逻辑的地方)
// app/controller/home.js
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
this.ctx.body = 'Hello world';
}
}
module.exports = HomeController;
步骤四:增加router(这个是路由的地方,/就是根目录,/news就是在url后面加上news的路径,找到对应的controller)
// app/router.js
module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
};
步骤五:配置config.default.js文件
'use strict';
module.exports = appInfo => {
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1111111111111_4659';
config.view = {
defaultViewEngine: 'nunjucks',
mapping: {
'.tpl': 'nunjucks',
},
};
return config;
};
步骤六:尝试运行(这里遇到了两个错误,后面有介绍)
npm run dev
步骤七:输入ip地址,就可以访问到一个有helloworld的网页了
下面需要与项目结合:(我需要在客户端访问服务器的接口获取一些数据)
步骤八:
增加路由:
'use strict';
/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
router.get('/config', controller.config.list);
};
步骤九:增加controller
//app/controller/config.js
const Controller = require('egg').Controller;
class NewsController extends Controller {
async list() {
const dataList = await this.other();
this.ctx.body = {
code:0,
masg:'news list success',
data:dataList
};
}
async other() {
return {
list: [
{ id: 1,test:"xxx"}]
}
}
}
module.exports = NewsController;
步骤十:增加egg-scripts模块(为了做线上部署)
npm i egg-scripts --save
步骤十一:添加 npm scripts
到 package.json
{
"scripts": {
"start": "egg-scripts start --daemon",
"stop": "egg-scripts stop"
}
}
步骤十二:部署上线:
npm start 开启
npm stop 关闭
功能一:增加post请求
1.增加routers
router.post('createPost', '/api/posts', controller.device.create);
2.增加处理文件device.js
const Controller = require('egg').Controller;
class PostController extends Controller {
async create() {
const { ctx, service } = this;
const createRule = {
title: { type: 'string' },
content: { type: 'string' },
};
ctx.logger.info('device.js info');
ctx.validate(createRule);
const author = ctx.session.title;
ctx.logger.info('author='+ctx.request.body.title);
const req = Object.assign(ctx.request.body, { author });
const res = await service.post.create(ctx.request.body);
ctx.body = { id: res.id };
ctx.status = 201;
}
}
module.exports = PostController;
3.增加service处理逻辑app/service/post.js
const Service = require('egg').Service;
class UserService extends Service {
async create(req) {
const { ctx, service } = this;
ctx.logger.info('author='+ctx.request.body.title);
const result = await this.app.mysql.insert('demo', { title: 'Hello World' });
return 1;
}
}
module.exports = UserService;
4.上面看到了我们加入了数据库,所以需要配置数据库
config.mysql = {
client: {
host: 'localhost',
port: '3306',
user: 'xxx',
password: 'xxx',
database: 'test',
},
app: true,
agent: false,
};
5.安装数据库插件
npm i --save egg-mysql
6.打开数据库://config/plugin.js
exports.mysql = {
enable: true,
package: 'egg-mysql',
};
在安装elasticsearch-head的过程中npm install遇到如下报错
[root@localhost elasticsearch-head]# npm install
npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.so.10 not defined in file libssl.so.10 with link time reference
网上找了一下相关资料,发现最直接有效的方法是升级openssl,执行yum update openssl -y命令即可。
升级之前版本信息:
[root@localhost elasticsearch-head]# openssl version
OpenSSL 1.0.1i 6 Aug 2014
升级之后:
[root@localhost elasticsearch-head]# openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017
此时再次执行,报错消除
[root@iz2zeevg2ntanmrgq9xdf5z node-egg]# npm run dev
> [email protected] dev /root/node-egg
> egg-bin dev
/root/node-egg/node_modules/egg/lib/start.js:5
module.exports = async (options = {}) => {
^
SyntaxError: Unexpected token (
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object. (/root/node-egg/node_modules/egg/index.js:17:17)
{ Error: /root/node-egg/node_modules/egg-bin/lib/start-cluster {"workers":1,"baseDir":"/root/node-egg","framework":"/root/node-egg/node_modules/egg"} exit with code 1
at ChildProcess.proc.once.code (/root/node-egg/node_modules/common-bin/lib/helper.js:56:21)
at ChildProcess.g (events.js:292:16)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:219:12) code: 1 }
npm ERR! Linux 3.10.0-514.10.2.el7.x86_64
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "run" "dev"
npm ERR! node v6.16.0
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! [email protected] dev: `egg-bin dev`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script 'egg-bin dev'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the egg package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! egg-bin dev
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs egg
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls egg
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /root/node-egg/npm-debug.log
原因:node.js版本过于老了,更新node.js版本
2019-06-02 16:19:51,442 WARN 9417 [-/127.0.0.1/-/5ms POST /device] missing csrf token. See https://eggjs.org/zh-cn/core/security.html#安全威胁csrf的防范
2019-06-02 16:19:51,444 WARN 9417 [-/127.0.0.1/-/7ms POST /device] nodejs.ForbiddenError: missing csrf token
at Object.throw (/root/egg-example/node_modules/koa/lib/context.js:97:11)
at Object.assertCsrf (/root/egg-example/node_modules/egg-security/app/extend/context.js:148:17)
at csrf (/root/egg-example/node_modules/egg-security/lib/middlewares/csrf.js:31:9)
at dispatch (/root/egg-example/node_modules/egg-security/node_modules/koa-compose/index.js:42:32)
at /root/egg-example/node_modules/egg-security/node_modules/koa-compose/index.js:34:12
at dispatch (/root/egg-example/node_modules/koa/node_modules/koa-compose/index.js:42:32)
at session (/root/egg-example/node_modules/koa-session/index.js:41:13)
at dispatch (/root/egg-example/node_modules/koa/node_modules/koa-compose/index.js:42:32)
at overrideMethod (/root/egg-example/node_modules/koa-override/index.js:38:12)
at dispatch (/root/egg-example/node_modules/koa/node_modules/koa-compose/index.js:42:32)
message: "missing csrf token"
pid: 9417
2019-06-02 16:42:54,061 INFO 9644 [master] egg started on http://127.0.0.1:7001 (1544ms)
2019-06-02 16:43:11,913 ERROR 9662 [-/127.0.0.1/-/8ms POST /api/posts?title=ewr&content=asdf] nodejs.TypeError: ctx.validate is not a function
at PostController.create (/root/egg-example/app/controller/device.js:9:9)
at Object.callFn (/root/egg-example/node_modules/egg-core/lib/utils/index.js:44:21)
at Object.classControllerMiddleware (/root/egg-example/node_modules/egg-core/lib/loader/mixin/controller.js:87:20)
at Object.callFn (/root/egg-example/node_modules/@eggjs/router/lib/utils.js:12:21)
at wrappedController (/root/egg-example/node_modules/@eggjs/router/lib/egg_router.js:322:18)
at dispatch (/root/egg-example/node_modules/koa-compose/index.js:44:32)
at next (/root/egg-example/node_modules/koa-compose/index.js:45:18)
at /root/egg-example/node_modules/@eggjs/router/lib/router.js:190:18
at dispatch (/root/egg-example/node_modules/koa-compose/index.js:44:32)
at /root/egg-example/node_modules/koa-compose/index.js:36:12
pid: 9662
1.增加egg-validate模块
npm i egg-validate --save
2.增加plugins.js
exports.validate = {
enable: true,
package: 'egg-validate',
};
2019-06-02 17:13:03,149 WARN 9845 [-/127.0.0.1/-/6ms POST /api/posts?title=ewr&content=asdf] nodejs.UnprocessableEntityError: Validation Failed
at Object.throw (/root/egg-example/node_modules/koa/lib/context.js:97:11)
at Object.validate (/root/egg-example/node_modules/egg-validate/app/extend/context.js:14:17)
at PostController.create (/root/egg-example/app/controller/device.js:9:9)
at Object.callFn (/root/egg-example/node_modules/egg-core/lib/utils/index.js:44:21)
at Object.classControllerMiddleware (/root/egg-example/node_modules/egg-core/lib/loader/mixin/controller.js:87:20)
at Object.callFn (/root/egg-example/node_modules/@eggjs/router/lib/utils.js:12:21)
at wrappedController (/root/egg-example/node_modules/@eggjs/router/lib/egg_router.js:322:18)
at dispatch (/root/egg-example/node_modules/koa-compose/index.js:44:32)
at next (/root/egg-example/node_modules/koa-compose/index.js:45:18)
at /root/egg-example/node_modules/@eggjs/router/lib/router.js:190:18
message: "Validation Failed"
code: "invalid_param"
errors: [{"message":"required","field":"title","code":"missing_field"},{"message":"required","field":"content","code":"missing_field"}]
pid: 9845
原因:这个使用postman上传的时候,报这个错,后来使用官方curl方式就没有问题了
curl -X POST http://47.xx.xx.xx/api/posts --data '{"title":"controller", "content": "what is controller"}' --header 'Content-Type:application/json; charset=UTF-8
[root@xiepengchong egg-example]# npm run dev
> [email protected] dev /root/egg-example
> egg-bin dev
2019-06-03 10:51:35,728 INFO 12643 [master] node version v10.16.0
2019-06-03 10:51:35,730 INFO 12643 [master] egg version 2.22.2
2019-06-03 10:51:36,319 INFO 12643 [master] agent_worker#1:12650 started (585ms)
2019-06-03 10:51:47,065 WARN 12661 [egg:core:ready_timeout] 10 seconds later /root/egg-example/node_modules/egg-mysql/lib/mysql.js:20:7 was still unable to finish.
2019-06-03 10:51:47,135 ERROR 12661 [-/127.0.0.1/-/0ms GET /] nodejs.PROTOCOL_SEQUENCE_TIMEOUTError: Handshake inactivity timeout
at Handshake. (/root/egg-example/node_modules/mysql/lib/protocol/Protocol.js:160:17)
at Handshake.emit (events.js:198:13)
at Handshake._onTimeout (/root/egg-example/node_modules/mysql/lib/protocol/sequences/Sequence.js:124:8)
at Timer._onTimeout (/root/egg-example/node_modules/mysql/lib/protocol/Timer.js:32:23)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
--------------------
at Protocol._enqueue (/root/egg-example/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/root/egg-example/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/root/egg-example/node_modules/mysql/lib/Connection.js:119:18)
at Pool.getConnection (/root/egg-example/node_modules/mysql/lib/Pool.js:48:16)
at /root/egg-example/node_modules/ali-rds/node_modules/pify/index.js:29:7
at new Promise ()
at Pool. (/root/egg-example/node_modules/ali-rds/node_modules/pify/index.js:12:10)
at Pool.ret [as getConnection] (/root/egg-example/node_modules/ali-rds/node_modules/pify/index.js:56:34)
at Pool.query (/root/egg-example/node_modules/mysql/lib/Pool.js:202:8)
at /root/egg-example/node_modules/ali-rds/node_modules/pify/index.js:29:7
sql: select now() as currentTime;
code: "PROTOCOL_SEQUENCE_TIMEOUT"
fatal: true
timeout: 10000
name: "PROTOCOL_SEQUENCE_TIMEOUTError"
pid: 12661
hostname: xiepengchong
2019-06-03 10:51:47,136 ERROR 12661 nodejs.PROTOCOL_SEQUENCE_TIMEOUTError: Handshake inactivity timeout
at Handshake. (/root/egg-example/node_modules/mysql/lib/protocol/Protocol.js:160:17)
at Handshake.emit (events.js:198:13)
at Handshake._onTimeout (/root/egg-example/node_modules/mysql/lib/protocol/sequences/Sequence.js:124:8)
at Timer._onTimeout (/root/egg-example/node_modules/mysql/lib/protocol/Timer.js:32:23)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
--------------------
at Protocol._enqueue (/root/egg-example/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/root/egg-example/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/root/egg-example/node_modules/mysql/lib/Connection.js:119:18)
at Pool.getConnection (/root/egg-example/node_modules/mysql/lib/Pool.js:48:16)
at /root/egg-example/node_modules/ali-rds/node_modules/pify/index.js:29:7
at new Promise ()
at Pool. (/root/egg-example/node_modules/ali-rds/node_modules/pify/index.js:12:10)
at Pool.ret [as getConnection] (/root/egg-example/node_modules/ali-rds/node_modules/pify/index.js:56:34)
at Pool.query (/root/egg-example/node_modules/mysql/lib/Pool.js:202:8)
at /root/egg-example/node_modules/ali-rds/node_modules/pify/index.js:29:7
sql: select now() as currentTime;
code: "PROTOCOL_SEQUENCE_TIMEOUT"
fatal: true
timeout: 10000
name: "PROTOCOL_SEQUENCE_TIMEOUTError"
pid: 12661
hostname: xiepengchong
2019-06-03 10:51:47,137 ERROR 12661 [app_worker] start error, exiting with code:1
[2019-06-03 10:51:47.149] [cfork:master:12643] worker:12661 disconnect (exitedAfterDisconnect: false, state: disconnected, isDead: false, worker.disableRefork: true)
[2019-06-03 10:51:47.149] [cfork:master:12643] don't fork, because worker:12661 will be kill soon
2019-06-03 10:51:47,150 INFO 12643 [master] app_worker#1:12661 disconnect, suicide: false, state: disconnected, current workers: ["1"]
[2019-06-03 10:51:47.150] [cfork:master:12643] worker:12661 exit (code: 0, exitedAfterDisconnect: false, state: dead, isDead: true, isExpected: false, worker.disableRefork: true)
2019-06-03 10:51:47,151 ERROR 12643 nodejs.AppWorkerDiedError: [master] app_worker#1:12661 died (code: 0, signal: null, suicide: false, state: dead), current workers: []
at Master.onAppExit (/root/egg-example/node_modules/egg-cluster/lib/master.js:426:21)
at Master.emit (events.js:198:13)
at Messenger.sendToMaster (/root/egg-example/node_modules/egg-cluster/lib/utils/messenger.js:137:17)
at Messenger.send (/root/egg-example/node_modules/egg-cluster/lib/utils/messenger.js:102:12)
at EventEmitter.cluster.on (/root/egg-example/node_modules/egg-cluster/lib/master.js:295:22)
at EventEmitter.emit (events.js:203:15)
at ChildProcess.worker.process.once (internal/cluster/master.js:190:13)
at Object.onceWrapper (events.js:286:20)
at ChildProcess.emit (events.js:198:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
name: "AppWorkerDiedError"
pid: 12643
hostname: xiepengchong
2019-06-03 10:51:47,152 ERROR 12643 [master] app_worker#1:12661 start fail, exiting with code:1
2019-06-03 10:51:47,153 ERROR 12643 [master] exit with code:1
2019-06-03 10:51:47,156 ERROR 12650 [agent_worker] receive disconnect event on child_process fork mode, exiting with code:110
2019-06-03 10:51:47,159 ERROR 12650 [agent_worker] exit with code:110
{ Error: /root/egg-example/node_modules/egg-bin/lib/start-cluster {"workers":1,"baseDir":"/root/egg-example","framework":"/root/egg-example/node_modules/egg"} exit with code 1
at ChildProcess.proc.once.code (/root/egg-example/node_modules/common-bin/lib/helper.js:56:21)
at Object.onceWrapper (events.js:286:20)
at ChildProcess.emit (events.js:198:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12) code: 1 }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `egg-bin dev`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2019-06-03T02_51_47_168Z-debug.log
解决办法:参考:https://stackoverflow.com/questions/33510184/change-mysql-root-password-on-centos7/34207996#34207996
1. Stop mysql:
systemctl stop mysqld
2. Set the mySQL environment option
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
3. Start mysql usig the options you just set
systemctl start mysqld
4. Login as root
mysql -u root
5. Update the root user password with these mysql commands
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
-> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit
*** Edit ***
As mentioned my shokulei in the comments, for 5.7.6 and later, you should use
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
Or you'll get a warning
6. Stop mysql
systemctl stop mysqld
7. Unset the mySQL envitroment option so it starts normally next time
systemctl unset-environment MYSQLD_OPTS
8. Start mysql normally:
systemctl start mysqld
Try to login using your new password:
7. mysql -u root -p
最后记录一下基本的数据库操作:
mysql -u root -p 输入用户名和密码
show databases; 显示所有的数据库
use test 使用test数据库
show tables; 查看数据库中表
select * from demo; 查看表中的内容
create database lotus;创建数据库
CREATE TABLE userInfo(userId varchar(255),active_time timestamp,openId varchar(255),earnTimes int,money int); //创建表,每次update都会更新active_time时间戳
CREATE TABLE userInfo(userId varchar(255),active_time timestamp DEFAULT CURRENT_TIMESTAMP,openId varchar(255),earnTimes int,money int);//只有创建才会更新active_time时间戳
drop table abc;//删除abc的table
DELETE FROM userInfo WHERE userId='sssssssss';//删除满足条件的行
alter table info MODIFY model VARCHAR(60); //当发现线上某一列类型错误的时候,修改
UPDATE pending SET action = 1 WHERE openId = 'ddafsdfasasdfsadf';//更新某一列的值
alter table pending add column request_time timestamp DEFAULT CURRENT_TIMESTAMP;//插入某一列
delete from money; //清空某个table
select * from userInfo where money is NULL;//某一列为null
select * from userInfo where money is not NULL;//某一列不为null